Shekar.gvr Shekar.gvr - 1 year ago 72
ASP.NET (C#) Question

Linq query with .Contains not returning any records

I have a default datatable on page load, further when i change the drop down selection on my page i want to loop through the existing datatable using the selected dropdown values.

As my drop downs are multiselect drop downs there is good chance of sending in multiple values to loop the datatable.

For this scenariao i used the linq qyery on the default datatable.
Like this:

var zones = from myRow in ds.Tables[1].AsEnumerable()
where myRow.Field<string>("BusinessUnitId").Contains(BUIds)
select myRow;
dt = zones.CopyToDataTable<DataRow>();

It works perfectly for single value of the drop down. But when multiple ids are passed, the query doesn't return and rows.

Note: BUIds is a string variable.(Example : "1,2")

Can some one tell me what i am missing in my code?

Answer Source

You are using Contains at the wrong place, it should be on the BUIds:

var ids = BUIds.Split(','); // get an Id array
var zones = from myRow in ds.Tables[1].AsEnumerable()
            where ids.Contains( myRow.Field<string>("BusinessUnitId") )
            select myRow;
dt = zones.CopyToDataTable<DataRow>();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download