We help IT Professionals succeed at work.

LINQ: Need to query on the fields, not the ID's

_myList.OrderBy(s => s .MyId).ThenBy(s => s.ValueType).ThenBy(s => s.qId)

_myList is a generic list which contains foreign keys, but I'd like to alphabetize the list by the values of the strings pointed to, not the ID's.

MyId and qId are forieng keys.  What's the linq method to include this relationship and be sorting by the actual string's value?

Thanks!
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
It sounds like you need a join. what is the name of the foreign table and the name of the string column?
curiouswebsterSoftware Engineer

Author

Commented:
Just use:

MyTable
MyStringField

to demonstrate the syntax for the for MyId value, which is a primary key in MyTable.
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
I believe this should do it.

_myList.Join(_myTable1, o => o.MyId, i => i.MyId, (outer, inner) => new { myList = outer, MyIdString = inner.MyStringField })
       .Join(_myTable2, o => o.myList.qId, i => i.qId, (outer, inner) => new { myList = o, QIdString = inner.MyStringField });
       .OrderBy(s => s.myList.MyIdString)
       .ThenBy(s => s.ValueType)
       .ThenBy(s => s.QIdString);

Open in new window

curiouswebsterSoftware Engineer

Author

Commented:
Thanks!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.