Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I send where statements to a method which are dynamically executed in a LINQ statement?
    primarykey
    data
    text
    <p>In the following example, <strong>GetFilteredCustomers()</strong> works fine so I can send various letters which I want customers to have in their last name.</p> <p><strong>But how can I build GetFilteredCustomersDynamic() which will enable me to send a full where clause that can be dynamically included in the LINQ statement?</strong></p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TestDynamicLinq2343 { public class Program { static void Main(string[] args) { List&lt;Customer&gt; customers = Customer.GetCustomers(); //semi-dynamic foreach (var customer in Customer.GetFilteredCustomers(customers, "o")) { Console.WriteLine(customer.LastName); } //fully-dyanmic (can send where clauses) foreach (var customer in Customer.GetFilteredCustomersDynamic(customers, c =&gt; c.FirstName.Contains("a"))) { Console.WriteLine(customer.LastName); } Console.ReadLine(); } } public class Customer { public string FirstName { get; set; } public string LastName { get; set; } public string Street { get; set; } public string Location { get; set; } public string ZipCode { get; set; } public static List&lt;Customer&gt; GetCustomers() { List&lt;Customer&gt; customers = new List&lt;Customer&gt;(); customers.Add(new Customer { FirstName = "Jim", LastName = "Jones" }); customers.Add(new Customer { FirstName = "Joe", LastName = "Adams" }); customers.Add(new Customer { FirstName = "Jake", LastName = "Johnson" }); return customers; } public static List&lt;Customer&gt; GetFilteredCustomers(List&lt;Customer&gt; customers, string letter) { return (from c in customers where c.LastName.Contains(letter) select c).ToList(); } public static List&lt;Customer&gt; GetFilteredCustomersDynamic(List&lt;Customer&gt; customers, Func&lt;..., bool&gt; whereClause) { return (from c in customers where ...whereClause... select c).ToList(); } } } </code></pre> <h1>Answer:</h1> <p>thanks elder_george and arjuns, I got this example to work like this (albeit without the <code>Expression&lt;&gt;</code> ):</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; namespace TestDynamicLinq2343 { public class Program { static void Main(string[] args) { List&lt;Customer&gt; customers = Customer.GetCustomers(); Func&lt;Customer, bool&gt; whereClause = c =&gt; c.LastName.Contains("a") &amp;&amp; c.FirstName.Contains("J"); foreach (var customer in Customer.GetFilteredCustomers(customers, whereClause)) { Console.WriteLine(customer.LastName); } Console.ReadLine(); } } public class Customer { public string FirstName { get; set; } public string LastName { get; set; } public string Street { get; set; } public string Location { get; set; } public string ZipCode { get; set; } public static List&lt;Customer&gt; GetCustomers() { List&lt;Customer&gt; customers = new List&lt;Customer&gt;(); customers.Add(new Customer { FirstName = "Jim", LastName = "Jones" }); customers.Add(new Customer { FirstName = "Joe", LastName = "Adams" }); customers.Add(new Customer { FirstName = "Jake", LastName = "Johnson" }); customers.Add(new Customer { FirstName = "Angie", LastName = "Reckar" }); customers.Add(new Customer { FirstName = "Jean-Luc", LastName = "Beaudoir" }); return customers; } public static List&lt;Customer&gt; GetFilteredCustomers(List&lt;Customer&gt; customers, Func&lt;Customer, bool&gt; whereClause) { return customers .Where(whereClause).ToList(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload