none
"The entity or complex type (Model.Type) cannot be constructed in a LINQ to Entities query" when grouping in EntityDataSource1_QueryCreated

    Question

  • Hi,

    I want to group (and filter) my Entity set in the QueryCreated event of my EntityDataSource.

    But I get the following error when trying to group the entities:

    "The entity or complex type DotNetNukeModel.WKD_PermitRequest cannot be constructed in a LINQ to Entities query"

    The EntityDataSource:

    <asp:EntityDataSource ID="EntityDataSource1" runat="server" 
      ConnectionString="name=DotNetNukeEntities" 
      DefaultContainerName="DotNetNukeEntities" EnableFlattening="False" 
      EntitySetName="WKD_PermitRequest" 
      onquerycreated="EntityDataSource1_QueryCreated">
    </asp:EntityDataSource>
    
    

    In EntityDataSource1_QueryCreated I want to group the entities on 3 fields:

    var permitrequests = e.Query.Cast<WKD_PermitRequest>();
    e.Query = from p in permitrequests
          group p by new { p.address, p.description, p.dossiernumber } into g
          select new WKD_PermitRequest
            {
              ID = g.Max(x=>x.ID),
              dossiernumber = g.Key.dossiernumber,
              description = g.Key.description,
              address = g.Key.address
            };
    
    


    When I execute this I get the above mentioned error message.

    How can I do grouping in the QueryCreated event of the EntityDataSource? (i.e. Not in the markup of the EntityDataSource.)

    Thanks!

    Friday, July 08, 2011 7:11 AM

Answers

All replies

  • Hi,

    You can't new an entity or complex type defined in your model, create a class outside the entity model, and new this instead, and everything should work.

     


    --Rune
    Friday, July 08, 2011 8:13 AM
  • Hi Rune,

     

    Thanks for the quick response!

    You say I have to new a new class (not being an Entity) and connect this (in the QueryCreated event) to the EntityDataSource?

    When I create a new class and put it's name instead of 'WKD_PermitRequest' after the 'select new' I get the error 'QueryCreated event returned a query of type 'ObjectQuery`1' when type 'ObjectQuery`1' is required.' . I get the same error when I don't specify any type name after 'select new'.

    Do I have to change anything to the configuration of the EntityDataSource?

    Regards,

    Friday, July 08, 2011 8:09 PM
  • Hi again!

    Well, I haven't worked with EDS, but I suspect that you can't use this if you don't attach a real EF result to it. You should try to use ObjectDataSource instead.


    --Rune
    Saturday, July 09, 2011 7:04 AM
  • Hi rikkert1,

    Welcome!

    I think @Rune is right, you can use Anonymous Type to store the results, I wrote the similiar code and it works.

    <asp:GridView ID="GridView1"
            runat="server" DataSourceID="EntityDataSource1">
          </asp:GridView>
        <asp:EntityDataSource ID="EntityDataSource1" runat="server" 
          ConnectionString="name=EFTestEntities" DefaultContainerName="EFTestEntities" 
          EnableFlattening="False" EntitySetName="Employees" EntityTypeFilter="Employee" 
          onquerycreated="EntityDataSource1_QueryCreated">
        </asp:EntityDataSource>
    

    ------------------------------------------

    protected void EntityDataSource1_QueryCreated(object sender, QueryCreatedEventArgs e)
        {
         var Employees= e.Query.Cast<Employee>();
         var test = from e1 in Employees
               group e1 by new { e1.EmployeeId, e1.Name } into g
               select new { EmployeeId = g.Key.EmployeeId, Name = g.Key.Name };
         List<Employee> list = new List<Employee>();
          foreach (var item in test)
         {
           list.Add(new Employee { EmployeeId=item.EmployeeId, Name=item.Name});
         }
        }
    

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, July 11, 2011 6:45 AM
    Moderator
  • Hi,

    Is there any updates from you, would you please feel free to let me know your feedback.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, July 15, 2011 3:28 AM
    Moderator
  • Hi Alen,

    I was unable to react to your suggestion because I was on holiday.

    But now work starts again and I've tried your solution. But I think I'm missing something.

    You fill the list, but how is the list connected back to the EntityDataSource? To the e.Query?

    Just setting

    e.Query = list.AsQueryable<WKD_PermitRequest>();
    
    

    produces the error: "QueryCreated event returned a query of type 'EnumerableQuery`1' when type 'ObjectQuery`1' is required"

    Please let me know.

    Thanks! 

     

    Monday, July 25, 2011 3:01 PM
  • Hi rikkert1,

    Thanks for your feedback.

    I'm sorry for my careless, I got to a wrong direction.

    The result type of the query that you assign to the e.Query parameter has to be the same type as the return type that is specified during the creation of the control, otherwise an exception will occur.(http://msdn.microsoft.com/en-us/library/ee404748.aspx).

    Employees---> is the type of System.Linq.IQueryable<WebApplication1.Employee> {System.Data.Objects.ObjectQuery<WebApplication1.Employee>}

    When we groupby the collection the return type is : System.Linq.IQueryable<<>f__AnonymousType0<int,string>> {System.Data.Objects.ObjectQuery<<>f__AnonymousType0<int,string>>}(http://msdn.microsoft.com/en-us/library/bb534492.aspx)

    In my first post, I have retrieve the records, so it is wrong, please igorn it.

    If you wan to use GroupBy function, you can use EDS's GroupBy property, you can refer here and find GroupBy keyword: http://www.asp.net/entity-framework/tutorials/the-entity-framework-and-aspnet-%E2%80%93-getting-started-part-3

    Thanks for understanding.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by rikkert1 Wednesday, July 27, 2011 7:00 PM
    Tuesday, July 26, 2011 9:17 AM
    Moderator
  • Hi Alan,

    OK, so

    Thanks for your info. And thanks for Rune's info as well.

    Cheers!

    Wednesday, July 27, 2011 7:00 PM