Answered by:
Using entity query OrderBy clause in include table

Question
-
Hi all,
I have a problem when building query having a OrderBy clause in include table:
I have 2 table: Vendor, VendorContacts with association: 1->*
1 Vendor has many VendorContacts. I need to show a vendor contact called Primary Contact in Vendor gridview
I want to building query for sorting function for First Name conlum in grid view :
I try this:
ObjectContext.Vendors.Include(v => v.VendorContacts).OrderBy(f => f.VendorContacts.FirstOrDefault(i => i.IsPrimaryContact).FirstName)
but it doesn't work,
Does any one have ideal for this,
Thanks you very much
- Moved by CoolDadTx Monday, November 18, 2013 4:05 PM EF related
Monday, November 18, 2013 4:04 AM
Answers
-
It is basically joined and orderby.
Take a look on this:http://www.codeimagine.com/2012/02/how-to-use-join-and-orderby-in-linq-to.html
chanmm
- Marked as answer by Fred Bao Monday, November 25, 2013 9:53 AM
Monday, November 18, 2013 4:04 PM -
Hello,
>>ObjectContext.Vendors.Include(v => v.VendorContacts).OrderBy(f => f.VendorContacts.FirstOrDefault(i => i.IsPrimaryContact).FirstName)
The OrderBy in the case will only work for properties in Vendors.
If you want to order by sub properties, we have to use LINQ lambda expression.
var result = (from school in db.Schools join student in db.Students on school.schoolId equals student.schoolId where student.IsPrimaryStudent == true orderby student.studentname descending select new { school.schoolId, school.schoolname, student.studentId, student.studentname }).ToList();
The result will be:
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.- Marked as answer by Fred Bao Monday, November 25, 2013 9:53 AM
Tuesday, November 19, 2013 9:34 AM
All replies
-
Try:
ObjectContext.Vendors.Include("VendorContracts") .Where(v => v.VendorContacts.IsPrimaryContact == true);
Bob - www.crowcoder.com
Monday, November 18, 2013 1:56 PM -
It is basically joined and orderby.
Take a look on this:http://www.codeimagine.com/2012/02/how-to-use-join-and-orderby-in-linq-to.html
chanmm
- Marked as answer by Fred Bao Monday, November 25, 2013 9:53 AM
Monday, November 18, 2013 4:04 PM -
Hello,
>>ObjectContext.Vendors.Include(v => v.VendorContacts).OrderBy(f => f.VendorContacts.FirstOrDefault(i => i.IsPrimaryContact).FirstName)
The OrderBy in the case will only work for properties in Vendors.
If you want to order by sub properties, we have to use LINQ lambda expression.
var result = (from school in db.Schools join student in db.Students on school.schoolId equals student.schoolId where student.IsPrimaryStudent == true orderby student.studentname descending select new { school.schoolId, school.schoolname, student.studentId, student.studentname }).ToList();
The result will be:
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.- Marked as answer by Fred Bao Monday, November 25, 2013 9:53 AM
Tuesday, November 19, 2013 9:34 AM