Linq query issues - runtime column definition

I have a linq question (linq to sql). I have the following code that works fine;

       var queryx = (from sa in d1.SampleAttributes
                      where nodeTable.ToList().Distinct().Contains(sa.client_post_code_prefix)
                     select sa.SampleId).Distinct();

Note: nodeTable is of type IQueryable

However, I would like to change this so that the column name inside the contains method can be resolved at runtime. I am defining a column name from another query depending on the custom filters applied, and ideally would like something with follwing logic;

// note that the row I pass gets the "column object" always has the same name as the column

        var columnWhatever = GetColumnName(string colName);

        var queryx = (from sa in d1.SampleAttributes
                      where nodeTable.ToList().Distinct().Contains(sa.client_post_code_prefix)
                     select sa.SampleId).Distinct();

So far, I have not been able to find anything that will allow this, and I'm starting to think that Linq does not allow such logic. Any help would be greatly appreciated

+3
2

Dynamic LINQ. . , . . .

+3

LINQ , , . :

string colName;
var queryx = (from sa in d1.SampleAttributes
              where nodeTable.Contains(
                                 sa.GetType()
                                   .GetProperty(colName)
                                   .GetValue(sa, null)
                                   .ToString()
                             )
              select sa.SampleId).Distinct();

, nodeTable IEnumerable<string>.

. , sa SampleAttribute. :

string colName;
PropertyInfo info = typeof(SampleAttribute).GetProperty(colName);
Func<SampleAttribute, string> func = sa => info.GetValue(sa, null).ToString();
var queryx = (from sa in d1.SampleAttributes
              where nodeTable.Contains(func(sa))
              select sa.SampleId).Distinct();

LINQ to SQL, , System.Linq.Expressions.Expression. nodeTable, .

+2
source

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


All Articles