大家好, 我在使用不等于(!=)条件的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不等于(!=)条件
发布评论