Dynamic Linq core

asp.net-core dynamic-linq linq

Question

Hi I am using a Jqwidgets Grid to display my data. It has a build in possibility to use filters but if you filter your records on the server side you have to build your own query. As I am working with Linq I thought to use the Dynamic Linq Library for Asp net core. Problem is there are not many examples or explanations how to do this. But I am busy for days now and not getting very far.The way I am setup; I have a normal Linq query:

 var Mut = from M in _DB.Mutations
                  join S in _DB.Shifts on M.ShiftId equals S.ShiftId
                  join U in _DB.RoosterUsers on M.UserId equals U.RoosterUserId
                  join D in deps on M.UserId equals D.UserId
                  join DD in _DB.Departements on D.DepartementID equals DD.DepartementId
                  select new MutationModel
                  {
                      MutId=M.MutationId,
                      Naam=U.FirstName + " " + U.LastName,
                      UserId=M.UserId,
                      Departement= DD.DepartementName,
                      MutationType = S.publicName,
                      MutationGroup = S.ShiftType.ToString(),
                      DateTot =M.DateTill,
                      TijdVan=M.DateStartOn,
                      TijdTot=M.DateTill,
                      Status=CreateStatus(M.Tentative, M.ApprovedOn, M.Processed, M.CancelRefId, M.Deleted)
                  };

This query is running OK and gives me all the data I need for the Grid.

Then for the filter I would like to add a dynamic Linq Query using the System.Linq.Dynamic.Core library But this is as far as I get things working until now:

var outQuery = Mut.Where("Status = @0 and UserId = @1", "Nieuw", "KLM22940").Select("Status");

My questions now : 1. In the where clause If I make the fieldname variable I get an error. how to do this?? 2. In the Select Clause, how to add multiple Columns? (actually I just like to output all columns.)

  1. Best would be to see an example. has somebody used Dynamic Linq to build a dynamic linq query for the JQWidgets Grid?

Thank you very much.

1
0
1/16/2020 6:40:13 PM

Popular Answer

In what way you are trying to use fieldname variable in where clause ?

If you want to output all columns you can use ToList() like

var outQuery = Mut.Where("Status = @0 and UserId = @1", "Nieuw", "KLM22940").ToList();

If you want to get some specific columns you can use Select clause like this

var outQuery = Mut.Where("Status = @0 and UserId = @1", "Nieuw", "KLM22940").Select("new(Status,UserId )");

This Select clause creates data class which contains Status and UserId properties and returns a sequence of instances of that data class.

0
1/16/2020 8:48:12 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