none
Cannot implicitly convert type 'System.Linq.IQueryable RRS feed

  • Question

  • I wrote the following compiled query:

    public static Func < Modal.Entities, string, IQueryable < Modal.Staff > > MyQuery = CompiledQuery.Compile((Modal.Entities U, string StaffNo) => U.Staff.Where(a => a.StaffNo == StaffNo));

    and used the following statement to invoke the compiled query:

    Modal.Staff abc = MyQuery(context, StaffNo);

    But I got the following error: Cannot implicitly convert type 'System.Linq.IQueryable ' to 'Modal.Staff'. An explicit conversion exists (are you missing a cast?)

    Can somebody help please??

    Many many thanks !!!

    Monday, February 10, 2014 8:29 AM

Answers

  • Hello,

    >>But I got the following error: Cannot implicitly convert type 'System.Linq.IQueryable ' to 'Modal.Staff'. An explicit conversion exists (are you missing a cast?)

    This is because MyQuery(context, StaffNo) will return a IQueryable collection type while abc is an entity type rather a collection.

    Have a try to change

    Modal.Staff abc = MyQuery(context, StaffNo); 

    to be

    IQueryable <Modal.Staff> abc = MyQuery(context, StaffNo); 

    You will get a collection.

    Or

    Modal.Staff abc = MyQuery(context, StaffNo). FirstOrDefault(); 

    You will get an entity.

    If I misunderstand, please let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, February 11, 2014 7:58 AM
    Moderator