SQL Where in Linq with DataTable

I am trying to achieve this in C #

Select a.Name,a.Param
from Customization a
where a.name in (select Name from Standard)

      

I tried something like this but it still doesn't work.

 merge = dt1.AsEnumerable()
            .Where(r => r.Field<string>("Name")
            .Contains(dt2.Rows.Contains("Name")))
            .CopyToDataTable();

      

+3


source to share


1 answer


Using the current way, we need to get the list of names from the second data table ( dt2

) for each row in dt1

so I suggest to get the list of names first and then check if the r.Field<string>("Name")

collection is contained. For this you can use the following code



var NameCollection = dt2.AsEnumerable().Select(x=> x.Field<string>("Name")).ToList();

merge = dt1.AsEnumerable()
           .Where(r => NameCollection.Contains(r.Field<string>("Name")))
           .CopyToDataTable();

      

+3


source







All Articles