0

I have this query:

(from mt in edmxObject.MyTable 
 where mt.Field1 != null 
 select mt.Field6)
.Distinct()
.ToList())

There are also field Field2, Field3, Filed4 and Field5 which a same in nature. They hold an integer number, but they can also be nullable.

I want to get whether some of the fields Field1, Field2, ..., Field5 has a value or they have a null value.

If I know the name of the column as a string like "FieldX" which is in the set (Field1, Field2, ..., Field5) how can I get a Linq query at runtime with the appropriate column and then execute it on my edmx model to get the value of the given field?

Alexandru Chichinete
  • 1,166
  • 12
  • 23
Vlad
  • 2,739
  • 7
  • 49
  • 100
  • This could be useful https://stackoverflow.com/questions/5026285/get-all-column-names-of-a-datatable-into-string-array-using-linq-predicate – Alen Genzić Jul 16 '18 at 12:33

1 Answers1

3

I have updated the sentence, basically added in the where clausule what I used in the select. I'm using reflection to get access to the properties and values.

        string fiealdName = "Field1";
        var neL = l.Where(f => f.GetType()
                                .GetProperty(fiealdName).GetValue(f, null) != null)
                   .Select(f => f.GetType()
                   .GetProperty(fiealdName).GetValue(f, null)
                   .GetValue(f, null))
                   .Distinct()
                   .ToList();
  • useful but actually i need to replace the where clause with column which name can be changed dynamically. – Vlad Jul 16 '18 at 12:44
  • Can you also add a code for how to add the returned items into a combobox? Linq seems confused with System.Reflection.PropertyInfo. This is the error I get: A first chance exception of type 'System.NotSupportedException' occurred in System.Data.Entity.dll Window_Initialized: LINQ to Entities does not recognize the method 'System.Reflection.PropertyInfo GetProperty(System.String)' method, and this method cannot be translated into a store expression. – Vlad Jul 16 '18 at 13:03
  • Are you using this query te retrieve data from a database? – Ruben Sebastian Jul 16 '18 at 13:11
  • Yes I have instantiated an object of the edmx model and use this object to access data from the database. MS SQL Server 2008 – Vlad Jul 16 '18 at 13:13
  • Than I should recommend to look at [here](https://stackoverflow.com/questions/22104050/linq-to-entities-does-not-recognize-the-method-system-object-getvalue) you have to build a LinQ Expression to make the where condition. – Ruben Sebastian Jul 16 '18 at 13:16
  • I actually tried this but I cannot get the data inside nel. var nel = edmxObject.MyTable.Where(k => k.GetType().GetProperty("Field2").GetValue(k, null) != null).Select(f => f.Field6); Don't know how. – Vlad Jul 16 '18 at 13:17
  • If is the case that you have two models. One for EntityFramework and Second to Domain. You could get all rows, parse to the Domain model and then filter. Is not recommended but if you don't have that many registers is a sort of solution – Ruben Sebastian Jul 16 '18 at 13:25