0

Im new in LINQ,i need to pass different conditions on the same LINQ query like:

  var rslt = (from t in cr.faultStat              
             where(parameter)
             select new{

             name=t.Name,
             Fname=t.fname

             });

the parameter seen in the where ,is the part i want to send conditions dynamicaly but i dont know how,because where accepts boolean,but i want to send something like:

 string parameter="date>"2018-10-10" && id="123"

generally how should i send parameter to linq dynamically

mortezasol
  • 851
  • 4
  • 14
  • Can you give some examples of dynamic parameters? There are different options, but they depend on the business case. I don't think you will get a general one-size-fits-all answer here. – Sefe Oct 12 '18 at 07:37
  • Actually .Where accepts `Expression>` – Fabjan Oct 12 '18 at 07:38
  • Not sure about this query syntax, but with method syntax you should be able to use Expressions for it – Tim Oct 12 '18 at 07:39
  • Have a look at: https://stackoverflow.com/questions/821365/how-to-convert-a-string-to-its-equivalent-linq-expression-tree – Rand Random Oct 12 '18 at 07:40
  • @Fabjan as far as i read Expression – mortezasol Oct 12 '18 at 07:40
  • I wonder why so many have this requirement. Why you need that? Why the user should be able to enter an arbitrary condition? Provide him with the available and then build the query. – Tim Schmelter Oct 12 '18 at 07:41
  • @TimSchmelter for diffrent condition but the same linq,whats your suggestion then?re-write everything for each condition? – mortezasol Oct 12 '18 at 07:42
  • @mortezasol - can you give an example of two or more different linqs that you wrote by hand and would like to generate dynamically? – Rand Random Oct 12 '18 at 07:47
  • i think what is meant is that the user can only manipulate the results using a well defined set of params. the basic selected column/fields returned tend to remain the same – Jazb Oct 12 '18 at 07:48
  • You can use LinqKit nuget package, you can find the documentation at https://github.com/scottksmith95/LINQKit#what-is-linqkit – lyz Oct 12 '18 at 13:23

1 Answers1

9

I guess (according to your comment) actually you don't have a string but you have multiple conditions. Then i'd suggest to write your query like this:

var query = cr.faultStat;
// the user selected a min-date-filter?
if(minDate.HasValue)
{
    query = query.Where(x => x.Date > minDate.Value);
}
// the user selected a max-date-filter?
if(maxInclusiveDate.HasValue)
{
    query = query.Where(x => x.Date <= maxInclusiveDate.Value);
}
// the user selected an id-filter?
if(id.HasValue)
{
    query = query.Where(x => x.Id == id.Value);
}

var rslt = query
    .Select(x => select new
    {
        t.name,
        t.fname, ...
    });

Actually after this the database (or whatever the source is) was not even queried once due to deferred execution of above methods. You have to use a foreach or a method that does it:

int resultCount = rslt.Count(); // like this
Tim Schmelter
  • 411,418
  • 61
  • 614
  • 859