usman habeeb
usman habeeb

Reputation: 21

Dynamic where clause in linq query

Sorry for my bad English. I have a problem. I want to create dynamic where clause in a LINQ query. I have one list object name "list1" having values Country, City, State and one datatable that has column named Name, Lastname, Country, City, State. I want to compare list1 values with datatable columns and get null / empty rows.

So I want a LINQ query like this:

var query = from p in datatable.AsEnumerable()
            where list1 == null
            select p 

but it returns an error. How can I solve this problem?

Thanks in advance.

Upvotes: 1

Views: 809

Answers (1)

TomTom
TomTom

Reputation: 62157

Ok, let's get going - your query is ridiculously bad.

  • You should not have datatable.AsEnumerable - that forces a table scan (running through the whole table).
  • Second, you have to code all fields expressively. This is going to get nasty - per definition Depending on size of list this will be very bad.

In general, every query is an IQueryable itself, so you can chain where conditions.VERY nice - I use that partially myself, defining the core query, then adding additional where clauses as needed (by input parameter) before executing.

Sadly, comparing a table against a list of elements by individual field match is as bad as it gets from the sql level.

Upvotes: 1

Related Questions