linq sort many 2 many question
- by user169867
My main entity is called [Contract]. Contract has a many 2 many relationship w/ [Service].
When I query for a list of Contracts I grab the 1st Service available like this:
q.Select(c =>
new ContractSearchResult()
{
ContractID = c.ContractID,
FirstService = c.Contract2Service.FirstOrDefault().Service.Name,
ServiceCount = c.Contract2Service.Count,
}
).ToList();
q is an IQueryable that has some filtering & paging already applied.
(When I display this list I show the FirstService if there's only 1. If 1 I show "My1stService (3)" to show I'm seeing the 1st of 3 services) This works fine.
My question is this:
Is there any way to sort by FirstService? Or is this impossible? I haven't found a way of expressing this in linq.
Any help would be appreciated.