Reputation: 11806
This is more of a brain teaser for me as the linqTOsql query is fulfilling the need. I am just curious as to how a query like this could be converted to lambda's or if that's even possible.
I had originally tried to accomplish this with lambdas, but the only way I could figure out how to accomplish the joins so I could order results correctly was to use the query syntax:
var entries = from e2c in entry2CatsTable
join sEntries in streamEntryTable
on e2c.streamEntryID equals sEntries.seID
orderby sEntries.seDateCreated descending
orderby e2c.e2cOrder
where e2c.catID == catID
select sEntries;
I had played around with this using lambdas and this is how far i got:
IQueryable<Entry2Cats> e2c = entry2CatsTable
.Where(x => x.catID == catID)
.OrderBy(x => x.e2cOrder);
IQueryable<StreamEntry> entries = e2c.SelectMany(x => x.StreamEntry);
These lambdas return the correct results but I couldn't order them correctly because I needed to order based on fields in the Entry2Cats
table and the StreamEntry
table. Because that was a many to many a statement like this doesn't work:
// doesn't work because it doesn't know which x.Entry2Cats.e2cOrder to use
entries.OrderByDescending(x => x.seDateCreated).OrderBy(x => x.Entry2Cats.e2cOrder);
Any thoughts?
Also, results need to be returned as IQueryable
because the controller adds pagination functionality.
Upvotes: 2
Views: 104
Reputation: 126952
var entries = entry2CatsTable
.Join(streamEntryTable, e2c => e2c.streamEntryID, sEntries => sEntries.seID, (e2c, sEntries) => new { e2c, sEntries })
.Where(item => item.e2c.catID == catID)
.OrderByDescending(item => item.sEntries.seDateCreated)
.ThenBy(item => item.e2c.e2cOrder)
.Select(item => item.sEntries);
Upvotes: 2