silverlight - Building Dynamic LINQ Queries based on Combobox Value -


I have a combo box in Silverlight that includes my LINQ-to-SQL objects (i.e. name, address, age, etc.). .) One of the properties of one has been created. I want to filter my results based on the selected value in the combo box.

Example: Say I want everyone with the last name "Smith" I select 'Last name' from the drop down list and enter Smith in a text box control. Normally I will type a LINQ query like this ...

var query = from the p
where p.ListName == textbox.Text
Select p;

Is it possible to dynamically fix assets, perhaps using reflection?

var query = in the p to
where p. (Dropdown value) == Text box. Text
Select P;

Assume:

  Public class person {public string LastName {Received; Set; }} IQueryable & lt; Person & gt; collection;  

Your query:

  var query = from the store p in the p.LastName == text box. Select text;  

This means:

  var query = archive. Where (P => P. listname == text box. Text);  

The compiler translates with an extension method:

  var query = Queryable.Where (collection, P => p.ListName == textbox .text);   

The second parameter of the cavalable. Where a expression & lt; Func & lt; Person, bool & gt; & Gt; . To represent the compiler lambda, expression & lt; & Gt; understands the type and code:

  using System.Linq.Expressions; Var query = queryable.Where (collection, expression, lambda & lt; func & amp; lt; person, bool & gt; expression.exle (expression.membermember entry (expression parameter (type (person), "p "Type" (type (person), typef (person) .GetProperty ("last name"), expression.makeemember avance (expression.content (text box), typef (text box) .GetProperty ("text"), expression ), "P"));  

This query means syntax.

You are free to call these methods yourself. To change the comparison property, change it:

  typef (person) .GetProperty ("LastName") < / Code> 

with:

  typef (person) .GetProperty (dropdown.selectedValue);  

Comments