locked
Iqueryable anonymous type assign to specific type RRS feed

  • Question

  • Dear All

    Thank you for your attention.

    My task is to query result using an input field, when the input field is not null, then join another table and get it filtered.

    If no input field is entered, just get whole summary.

    My works are here

    IQueryable<Summary> query = context.Summaries;
    
    if (inputField != null)
    
    {
    
           var tempQuery = query.Join(context.Others.Where(o=>o.field == inputField),
    
    	 a => a.ID,
             b => b.SummaryID,
            (a, b) => new
                                {
                                    a
                                }); 
    
    
          query = tempQuery;   <-- Here got a problem
    
    }

    tempQuery is IQueryable<AnonymousType> but query is IQueryable<Summary>. cannot compile  here

    query = (IQueryable<Summary>)tempQuery;  explict casting is aslo not working

    How could I enhance he above program? thanks


    Best Regards
    mintssoul


    • Edited by mintssoul Saturday, May 9, 2015 3:52 AM
    Saturday, May 9, 2015 3:52 AM

Answers

  • tempQuery is IQueryable<AnonymousType> but query is IQueryable<Summary>. cannot compile  here

    It's telling you it can't make the cast. You have an object car,  and you are telling car  you are going to make it a horse.  Both objects may provide transportation, but they are two different objects.

    Also, Summary is a concrete class, and it can't be casted over to an AnonymousType object.

    https://msdn.microsoft.com/en-us/library/bb397696.aspx?f=255&MSPPError=-2147217396

    • Marked as answer by mintssoul Monday, May 11, 2015 4:36 AM
    Saturday, May 9, 2015 4:30 PM

All replies

  • tempQuery is IQueryable<AnonymousType> but query is IQueryable<Summary>. cannot compile  here

    It's telling you it can't make the cast. You have an object car,  and you are telling car  you are going to make it a horse.  Both objects may provide transportation, but they are two different objects.

    Also, Summary is a concrete class, and it can't be casted over to an AnonymousType object.

    https://msdn.microsoft.com/en-us/library/bb397696.aspx?f=255&MSPPError=-2147217396

    • Marked as answer by mintssoul Monday, May 11, 2015 4:36 AM
    Saturday, May 9, 2015 4:30 PM
  • tempQuery is IQueryable<AnonymousType> but query is IQueryable<Summary>. cannot compile  here

    It's telling you it can't make the cast. You have an object car,  and you are telling car  you are going to make it a horse.  Both objects may provide transportation, but they are two different objects.

    Also, Summary is a concrete class, and it can't be casted over to an AnonymousType object.

    https://msdn.microsoft.com/en-us/library/bb397696.aspx?f=255&MSPPError=-2147217396

    Dear DA924

    Thank you for your reply.  The concept IQueryable<AnonymousType> could not convert back to IQueryable<Summary>  is good.

    I am now using DTO for manipulating the intermediate result, thanks.


    Best Regards
    mintssoul


    • Edited by mintssoul Monday, May 11, 2015 2:57 AM
    Monday, May 11, 2015 1:27 AM
  • tempQuery is IQueryable<AnonymousType> but query is IQueryable<Summary>. cannot compile  here

    It's telling you it can't make the cast. You have an object car,  and you are telling car  you are going to make it a horse.  Both objects may provide transportation, but they are two different objects.

    Also, Summary is a concrete class, and it can't be casted over to an AnonymousType object.

    https://msdn.microsoft.com/en-us/library/bb397696.aspx?f=255&MSPPError=-2147217396

    Dear DA924

    Thank you for your reply.  The concept IQueryable<AnonymousType> could not convert back to IQueryable<Summary>  is good.

    I am now using DTO for manipulating the intermediate result, thanks.


    Best Regards
    mintssoul


    A DTO is a concrete class. :)
    Monday, May 11, 2015 4:03 AM