Search code examples
c#linqiqueryabledynamic-linq

C# Dynamic LINQ Contains using an array only evaluates the first value


Relatively new to C# and started using Dynamic LINQ to filter a data table adapter using a string. The issue I'm having is that the Where clause only seems to evaluate the first value in the string and none of the others. Here is the code I am using.

        string[] ids = new string[] { "12345", "67891", "45878" };
        var resultQ = (from pr in table1 select pr).AsQueryable();
        var iq = resultQ.Where("@0.Contains(FieldName)", ids);  

It works but only for the first value "12345" so the output of iq displays all fields for "12345". I tried using linq.dynamic.core to see if that would help but the still same result (or I haven't used it properly). I know I'm probably missing something minor here but any help would be greatly appreciated.

Also on a separate note: I wanted to convert the end result of iq which is a IQueryable type to EnumerationRowCollection type. Is this possible?

Thanks in advance


Solution

  • Managed to fix both points now. Either set string[] to string for dynamic LINQ to get all values in list as coded below

    string ids =  "12345,67891,45878";
    var resultQ = (from pr in table1 select pr).AsQueryable();
    var iq = resultQ.Where("@0.Contains(FieldName)", ids); 
    

    or use Syed's suggestion and change the LINQ query and keep the array (thanks again Seyed)

    For the conversion of IQueryable type to EnumerationRowCollection I changed EnumerationRowCollection to IEnumerable and this worked for all my LINQ queries

    Thanks