Linq variable access property

Say I have a class like:

public class Foo { public string Title {get;set;} } 

Now suppose I have a public List<Foo> myList that I want to filter Linq like this:

 var x = myList.Where(f => f.Title == myValue); 

Everything is good and clear so far.

But how to access the property of a variable? Sort of:

 string myProperty = "Title"; var x = myList.Where(f => f.myProperty == myValue); 
+6
source share
5 answers

You can write an extension method

 public static class MyExtensions { public static object GetProperty<T>(this T obj, string name) where T : class { Type t = typeof(T); return t.GetProperty(name).GetValue(obj, null); } } 

and use it like this:

 var x = myList.Where(f => f.GetProperty("Title") == myValue); 
+12
source

This is not a situation where LINQ is used. LINQ is a convenient collection management interface. Access to members through a textual representation is provided with reflection.

 object GetProperty(Foo f, string propertyName) { var type = typeof(Foo); var propInfo = type.GetProperty(propertyName); return propInfo.GetValue(f, null); } 
+3
source

I know this is an old thread, but here is another way to do it. The advantage of this is that it is significantly faster if you need to do this in a loop. I converted the result from "func" to an object to make it more general.

  var p = Expression.Parameter(typeof(string)); var prop = Expression.Property(p, "Length"); var con = Expression.Convert(prop, typeof(object)); var exp = Expression.Lambda(con, p); var func = (Func<string, object>)exp.Compile(); var obj = "ABC"; int len = (int)func(obj); 

In the original question, the code was used inside linq, so the speed may be good. One could use "func" directly in the where clause if it was built correctly, for example

  class ABC { public string Name { get; set; } } var p = Expression.Parameter(typeof(ABC)); var prop = Expression.Property(p, "Name"); var body = Expression.Equal(prop, Expression.Constant("Bob")); var exp = Expression.Lambda(body, p); var func = (Func<ABC, bool>)exp.Compile(); ABC[] items = "Fred,Bob,Mary,Jane,Bob".Split(',').Select(s => new ABC() { Name = s }).ToArray(); ABC[] bobs = items.Where(func).ToArray(); 
+2
source

If you need to compose your queries dynamically on the fly, you can use LINQ Dynamic Query , a sample from Microsoft:

This example shows how to compose LINQ statements on a fly, dynamically, at run time.

Link to the library in your code:

 using System.Linq.Dynamic; 

Your request will look like this:

 // You can use a string as the argument for the Where method // meaning you can compose this string dynamically string myProperty = "Title"; var x = myList.Where(myProperty + " = " + myValue); 

You can also use placeholder in the query string, which improves readability (several):

 var x = myList.Where("@0 = @1", myProperty, myValue); 

See also this post by Scott Guthrie: Dynamic LINQ Part 1: Using the LINQ Dynamic Query Library (I don't think there has ever been part 2 ...)

Note: you need to compile sample code from Microsoft and reference the built-in assembly, or you can include the code in your own project.

+1
source

you cannot use linq dynamic query from microsoft here is sample code

  var query = db.Customers.Where("City == @0 and Orders.Count >= @1", "London", 10). OrderBy("CompanyName"). Select("New(CompanyName as Name, Phone)"); 
0
source

Source: https://habr.com/ru/post/905404/


All Articles