By kds6253


2012-01-03 20:55:40 8 Comments

I am trying to make a LINQ statement where the where clause comes from a variable. For example:

string whereClause = address.zip == 23456;
var x = from something in someList where whereClause;

Is this possible? I cannot seem to get it to work.

thanks,

Update - my where clause is predefined and will be based on user input so I don't think this will work for me. Basically whereClause is not constructed in the method, it is a parameter of the method which does the LINQ. I didn't explain that well here is a better example:

public void doLnq(string whereClause)
{
   var x = from something in someList where whereClause;
   dowork(x);
}

Update - Just to sum up some of the suggestions and centralize everything.

I cannot use a switch to generate the where clause because there are way to many possibilities.

The dynamic linq post that a few of you have posted does look promising but i am having trouble related the linq to sql example to my linq to objects problem.

and @sLaks after looking through msdn http://msdn.microsoft.com/en-us/library/bb353734.aspx I am having trouble figuring out where you meant to use AsQueryable

thanks,

4 comments

@afrischke 2012-01-03 21:59:12

As Richard has pointed out, the Dynamic Query Library can be used to build dynamic filter expressions. When using Linq-To-Objects make sure to convert your IEnumerable<T> to a IQueryable<T> first. Here is an (incomplete) example:

using System.Linq.Dynamic;

namespace System.Linq.Dynamic
{
  public class Example
  {
   // Assuming some value is assigned to below field somewhere... 
   private IEnumerable<Address> m_Addresses;

   public void FilterByZipCode(string zipCode)
   {
      var x = m_Addresses.AsQueryable().Where("Zip == @0", zipCode);
      dowork(x);
   }
  }

  public class Address
  {  
     public String Zip { get; set; }

     // More Properties...  
  }
}

@SLaks 2012-01-03 20:56:47

You need to assembly an Expression<Func<T, bool>> and pass it to the Where() extension method:

Expression<Func<T, bool>> whereClause = a => a.zip == 23456;
var x = frSomeList.Where(whereClause);

EDIT: If you're using LINQ to Objects, remove the word Expression to create an ordinary delegate.

@Anthony Pegram 2012-01-03 21:01:45

With a linq-to-objects tag, would an Expression be appropriate?

@kds6253 2012-01-03 21:06:21

please see update

@SLaks 2012-01-03 21:08:50

@kds6253: Then you can take a delegate as a parameter. If you need to use a string, you can use dynamic LINQ (but it will be slower)

@kds6253 2012-01-03 21:11:03

speed shouldn't be an issue, they will be relatively simple queries. Would you happen to have a short example of dynamic linq

@kds6253 2012-01-03 21:22:44

please refer to my comment on the similar answer below. thanks.

@SLaks 2012-01-03 21:27:57

@kds6253: Just call AsQueryable()

@kds6253 2012-01-03 22:16:47

this solution works. Thanks to @sq33G for helping out as well.

@Francis Ducharme 2014-07-04 16:18:43

@SLaks That for that. T is used to qualify the expression that's being created, but what does the bool mean exactly ?

@SLaks 2014-07-04 16:19:30

@FrancisDucharme: Wrong. T and bool are the type parameters of the Func<,> delegate. T is the parameter type; bool is the return type. (see the delegate declaration)

@asem mokllati 2017-07-07 16:43:21

guys if whereClause take two parameters, how to use it in where clause @SLaks

@SLaks 2017-07-07 17:25:34

@asemmokllati: Your question makes no sense. Where() only passes one parameter (unless you mean the index, in which case just make an expression that takes two parameters).

@dtb 2012-01-03 20:57:20

This:

var query = from something in someList where whereClause;

is shorthand for:

var query = someList.Where(something => whereClause);

Assuming someList is an IEnumerable<Address>, Where refers to the Enumerable.Where Extension Method. This method expects a Func<Address, bool> which you can define as follows:

Func<Address, bool> whereClause = address => address.Zip == 23456;
var query = someList.Where(whereClause);

@kds6253 2012-01-03 21:05:45

please see update

@Rob Windsor 2012-01-03 20:57:55

That's a built-in Feature of LINQ. Just use the Where extension method.

See LINQ Query Syntax versus Method Syntax (C#) for more information.

Related Questions

Sponsored Content

26 Answered Questions

[SOLVED] How do I enumerate an enum in C#?

61 Answered Questions

[SOLVED] What is the difference between String and string in C#?

9 Answered Questions

[SOLVED] Group by in LINQ

  • 2011-09-06 19:44:20
  • test123
  • 1133741 View
  • 940 Score
  • 9 Answer
  • Tags:   c# linq group-by

63 Answered Questions

[SOLVED] How do I calculate someone's age in C#?

  • 2008-07-31 23:40:59
  • Jeff Atwood
  • 541011 View
  • 1742 Score
  • 63 Answer
  • Tags:   c# .net datetime

9 Answered Questions

[SOLVED] What are the correct version numbers for C#?

296 Answered Questions

[SOLVED] Hidden Features of C#?

  • 2008-08-12 16:32:24
  • Serhat Ozgel
  • 661649 View
  • 1476 Score
  • 296 Answer
  • Tags:   c# hidden-features

24 Answered Questions

[SOLVED] Cast int to enum in C#

  • 2008-08-27 03:58:21
  • lomaxx
  • 1185519 View
  • 2870 Score
  • 24 Answer
  • Tags:   c# enums casting

21 Answered Questions

[SOLVED] LINQ query on a DataTable

19 Answered Questions

[SOLVED] Dynamic LINQ OrderBy on IEnumerable<T> / IQueryable<T>

7 Answered Questions

[SOLVED] Multiple "order by" in LINQ

  • 2008-11-18 13:34:11
  • Sasha
  • 552996 View
  • 1486 Score
  • 7 Answer
  • Tags:   linq sql-order-by

Sponsored Content