2

I need to find data from data table with multiple values from one column only.

Below is an example:

 var Value = (from r in datatable.AsEnumerable()
             where r.Field<string>(ColumnName).Contains("test1,test2")
             select r ).ToList();

Here ColumnName is my Datatable's column name and "test1, test2" are different values ( number of values are dynamic and have "," as delimiter ).

I want to make this Linq query which returns all records from datatable which have values equal to "test1" and "test2".

Edit : Above LINQ Query returns result like below SQL Query. Select * from Table where ColumnName in ('test1','test2')

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
Jankya
  • 966
  • 2
  • 12
  • 34

7 Answers7

2

Use Split + Join:

IEnumerable<string> values = "test1,test2".Split(',');
var matchingRows = from row in datatable.AsEnumerable()
                   join value in values
                   on row.Field<string>(ColumnName) equals value
                   select row;
DataTable tblResult = matchingRows.CopyToDataTable(); // or ToList

For the sake of completeness, the less efficient but more natural approach using Contains:

var matchingRows = from row in datatable.AsEnumerable()
                   where values.Contains(row.Field<string>(ColumnName))
                   select row;
Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

Build a list :

var filters = new [] {"test1", "test2"};

var Value = (from r in datatable.AsEnumerable()
             where filters.Contains(r.Field<string>(ColumnName))
             select r ).ToList();

This will be translate to what you want

Select * 
from Table 
where ColumnName in ('test1','test2')
Cyril Gandon
  • 16,830
  • 14
  • 78
  • 122
-1

Try this :

var searchParameters = new []{ "test1", "test2" }; // or "test1,test2".Split(",")
var Value = (from r in datatable.AsEnumerable()
         where searchParameters.All(s => r.Field<string>(ColumnName).Contains(s))
         select r).ToList();
Réda Mattar
  • 4,361
  • 1
  • 18
  • 19
-1

The clean and easy way would be to use Select.

Example:

var dt = new DataTable();
dt.Columns.Add("col1", typeof(string));
dt.Rows.Add(new object[] {"test1"});
dt.Rows.Add(new object[] {"test2"});
dt.Rows.Add(new object[] {"test3"});
dt.Rows.Add(new object[] {"test4"});

// Select * from Table where ColumnName in ('test1','test2')
var rows = dt.Select("col1 in ('test1','test2')");

rows is now:

enter image description here

sloth
  • 99,095
  • 21
  • 171
  • 219
-1

You can you or statement ' || '

 var Value = (from r in datatable.AsEnumerable()
                             where r.Field<string>(ColumnName).Contains(value1) || r.Field<string>(ColumnNamedatatable).Contains(value2)
                             select r).ToList();
-1
datatable.where(r=>{"test1","test2"}.Contains(r.ColumnName))
invictus
  • 93
  • 1
  • 2
  • 8
-2

try this:

var Value = (from r in datatable.AsEnumerable()
                 where r.Field<string>(ColumnName).Contains("test1").Contains("test2")
                 select r ).ToList();
Arnold
  • 88
  • 1
  • 3