Work with lists without known columns

1

I have a form that receives a list, to show on the screen and allow to choose, or even, to search in it, something simple to not use heavy and expensive controls like DevExpress , the subject is the following:

The form receives the list, the names of columns and the size of columns, everything goes well, but now I have added a column that should be sent and that is the sum of all the previous fields, so that it can be done a search on that single column and the matching rows can be filtered.

This column I have called ItemString and the linq goes like this:

private ICollection<dynamic> Listado;

...

var lista = (from i in this.listado
             where (i.ItemString.Contains(sValorBuscado))
             select i).ToList();

and throws me the error:

  

'object' does not contain a definition for 'ItemString'   System.Collections.ListDictionaryInternal

How can this be done? How can this be avoided, given that the names of the columns are not known? In the debugging I see that if the ItemString column arrives, but the linq does not seem to recognize it.

    
asked by Marco Antonio Garcia Leon 25.08.2017 в 18:21
source

1 answer

0

Since you use a type dynamic , which finally when used in Linq is an anonymous type, you must declare the variables with which you are working:

var lista = (from i in this.listado
             where (i.ItemString.Contains(sValorBuscado))
             select new {
                 ItemString = i.ItemString
                 //Aquí pones el resto de las propiedades
             }).ToList();

With this, you are explicitly saying which columns that structure should work with. The truth is that it would be a genius that the anonymous data structure automatically detects data of type dynamic .

    
answered by 25.08.2017 в 22:11