Linq不等于(!=)条件

编程入门 行业动态 更新时间:2024-10-23 19:22:25
本文介绍了Linq不等于(!=)条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 我在使用不等于(!=)条件的lambda(或linq)中遇到问题. 这是我的代码:

Hi guys, I have a problem in lambda(or linq) using not equal(!=) condition. Here''s my code:

IEnumerable<DataRow> ToUpload = dtTable.Select().Where(o => o[0] != dtUploadedIDs.Select().Where(p => (string)p[1] == sTableName).Select(q => q[0]));

dtTable看起来像这样:

dtTable looks like this:

-------------------- | IndexID | blabla -------------------- | 1 | asd | 2 | rtr --------------------

和dtUploadedIDs像这样:

and dtUploadedIDs is like this:

--------------------- | IndexID | TableName --------------------- | 1 | asd ---------------------

我想删除dtTable中IndexID等于dtUploadedIDs索引ID的所有行. 它们都在DataTable中. 请帮帮我. 在此先感谢您.

I want to remove all rows in dtTable whose IndexID is equal to dtUploadedIDs Index ID. They are both in DataTable. Please help me. Thanks in advance.

推荐答案

我将解决方案分为两个变量,以便更加清楚地说明, 首先,让我们获取所有ID(我想它是int类型,而不是字符串或其他任何类型): I''ve split my solution into two variables to be more clear, First thing, lets get all ids (I suppose it is type int, not string or any other): var allUploaded = dtUploadedIDs.Select().Select( p => ( int )p[ 0 ] );

拥有所有ID后,我们可以检查ToUpload表中的ID是否等于其中任何一个:

After we have all ids we can check whether our id from ToUpload table equals to any of them:

var ToUpload = dtTable.Select().Where( o => !allUploaded.Contains( ( int )o[ 0 ] ) );

而已.您可以根据需要将它们组合为单个表达式.

That''s it. You can combine them in single expression if you wish.

尝试此LINQ查询 Try this LINQ query // Select Distinct and Index ids that are only found in dtTable var indexIDs =(from row1 in dtTable.AsEnumerable() select new { indexID1 = row1.Field<string>("IndexID"), }).Distinct.Except(from row2 in dtUploadedIDs.AsEnumerable() select new { indexID2 = row2.Field<string>("IndexID") })); // Select the record based on the IndexId results var toUpload = (from row1 in dtTable.AsEnumerable() select new { indexID = row1.Field<string>("IndexID"), blabla = row1.Field<string>("blabla") }).where(r=> r.indexIDs.Contains(indexID)));

有关更多信息,请参见 101个LINQ示例:设置运算符[不同且不相同] [ ^ ] 在DataView中查询DataRowView集合 [ ^ ]

For more info look 101 LINQ Samples: Set Operators [ Distinct and Except ][^] Querying the DataRowView Collection in a DataView[^]

更多推荐

Linq不等于(!=)条件

本文发布于:2023-11-15 21:38:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1598181.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不等于   条件   Linq

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!