LINQ - How to join multiple tables and order by dynamically?

c# dynamic-linq linq

Question

I've definitely seen similar questions, but for some reason, I can't get this query to work correctly. I'm using System.Linq.Dynamic.Core and trying to dynamically order the results based on user input. The "PatternName, Model" string in the examples will normally be a variable. Here is what I've tried.

IQueryable sortedResults = from reqs in dataContext.Requirements
                                   join pats in dataContext.Patterns on reqs.PatternRequirement equals pats.Id
                                   join sups in dataContext.Suppliers on reqs.SupplierRequirement equals sups.Id
                                   orderby ("PatternName, Model")
                                   select new
                                   {
                                       reqs.Id,
                                       sups.SupplierName,
                                       pats.PatternName,
                                       reqs.Model,
                                       reqs.YardageRequirements,
                                       reqs.Repeat,
                                       reqs.ModelDescription,
                                       reqs.CreateUser,
                                       reqs.CreateTimeStamp,
                                       reqs.UpdateUser,
                                       reqs.UpdateTimeStamp
                                   };

return sortedResults;

This compiles and runs without error, but my result set is still not ordered the way I need it to be. I've also tried:

IQueryable sortedResults = from reqs in dataContext.Requirements
                                   join pats in dataContext.Patterns on reqs.PatternRequirement equals pats.Id
                                   join sups in dataContext.Suppliers on reqs.SupplierRequirement equals sups.Id
                                   select new
                                   {
                                       reqs.Id,
                                       sups.SupplierName,
                                       pats.PatternName,
                                       reqs.Model,
                                       reqs.YardageRequirements,
                                       reqs.Repeat,
                                       reqs.ModelDescription,
                                       reqs.CreateUser,
                                       reqs.CreateTimeStamp,
                                       reqs.UpdateUser,
                                       reqs.UpdateTimeStamp
                                   };

sortedResults.OrderBy("PatternName").ThenBy("Model");

return sortedResults;

This also runs without error, but produces the same result. Most of the examples I have seen using Dynamic Linq use method syntax, but I am unsure of how to accomplish writing my inner joins doing it that way. Does it have to be done using method syntax? And if it does, can you provide an example of joining on multiple tables?

Any help is appreciated.

1
1
5/31/2018 1:13:24 PM

Accepted Answer

The

sortedResults.OrderBy("PatternName").ThenBy("Model"); 

is wrong!

sortedResults = sortedResults.OrderBy("PatternName").ThenBy("Model"); 

Linq methods don't modify the query, instead return a new query that is different from the original.

You can't use dynamic linq through the use of the linq syntax. You have to use the functional syntax.

1
5/31/2018 1:14:36 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow