使用Linq过滤使用“IN”的数据表和“不在”条款 [英] Filter Using Linq On datatable with "IN" and "Not In" Clause

查看:82
本文介绍了使用Linq过滤使用“IN”的数据表和“不在”条款的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,

我想基于某些条件过滤数据表行我尝试使用以下代码来解雇!和IN条款。



  var  deepak = 来自 n   datatable.AsEnumerable()
其中!( from n1 in datatable.AsEnumerable()
选择 n1.Field< string>( stauscode)。包含( JOB))
select n;





请建议我如何在数据表中放置linq中的in和not in条件

i也试过谷歌搜索,但这不是很容易理解。



先谢谢

解决方案

尝试

  var  list1 = 来自 n1   datatable.AsEnumerable() select  n1.Field< string> (  stauscode)。包含(  JOB

var list2 = 来自 n datatable.AsEnumerable()选择 n

var result = list2.Except(list1);


从问题出现来自 datatable 的所有行,在 stauscode JOB c>列需要过滤掉,因为内部查询中使用了相同的 datatable 。如果是这种情况,那么我认为可以使用以下查询。

  var  filteredRows = < span class =code-sdkkeyword> from  n  in  datatable.AsEnumerable()
其中!n.Field< string>( stauscode)。包含( JOB
select n;



如果结果需要 DataTable 那么

  var  filteredTable =( from  n  in  datatable.AsEnumerable()
其中!n.Field< string>( < span class =code-string> stauscode).Contains( JOB
选择 n).CopyToDataTable();


Hi Friends,
I want to filter the rows of data table based on some condition i have tried the following code to fire with "!" and "IN" clause.

var deepak = from n in datatable.AsEnumerable()
                        where !(from n1 in datatable.AsEnumerable()
                                select n1.Field<string>("stauscode").Contains("JOB"))
                        select n;



Please suggest me how to place the in and not in condition in linq on datatable
i have also tried googling but that are not very much understandable.

Thanks in Advance

解决方案

Try

var list1 = from n1 in datatable.AsEnumerable() select n1.Field<string> ("stauscode").Contains("JOB")

var list2 = from n in datatable.AsEnumerable() select n

var result = list2.Except(list1);


From the question it appears that all rows from datatable, which do not contain JOB in stauscode column are required to be filtered out, as the same datatable is used in the inner query. If that is the case, then I think the following query can be used.

var filteredRows = from n in datatable.AsEnumerable()
                   where !n.Field<string>("stauscode").Contains("JOB")
                   select n;


If the result is required as a DataTable then

var filteredTable = (from n in datatable.AsEnumerable()
                    where !n.Field<string>("stauscode").Contains("JOB")
                    select n).CopyToDataTable();


这篇关于使用Linq过滤使用“IN”的数据表和“不在”条款的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆