linq sort many 2 many question

Posted by user169867 on Stack Overflow See other posts from Stack Overflow or by user169867
Published on 2010-03-30T13:21:57Z Indexed on 2010/03/30 13:23 UTC
Read the original article Hit count: 432

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about LINQ

Related posts about linq-to-entities