Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 looks like this:
--------------------
| IndexID |  blabla
--------------------
|    1    |   asd
|    2    |   rtr
--------------------


and dtUploadedIDs is like this:
---------------------
| IndexID | TableName
---------------------
|    1    |   asd
---------------------


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.
Posted

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 ] );


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.
 
Share this answer
 
Comments
unknowndentified10111 15-Jul-11 0:16am    
I already try the contains but it throws an error:
{"Specified cast is not valid."}

dtTable is from MySQL, IndexID is in int(11) data type.
dtUploadedIDs is from MSSQL, IndexID is in string data type but you can convert it to int since it only contain numbers.

What could be the problem?
skv_lviv 15-Jul-11 0:23am    
You have different types for IndexId column, which isn't quite good. The best thing is to change type of dtUploadedIDs.IndexID to int in the database.
However if this isn't possible, then you have two choices.

1) convert ids from allUploaded to int, this will look like
var allUploaded = dtUploadedIDs.Select().Select( p => Convert.ToInt32( p[ 0 ] ) );

or

2) You may convert dtTable.IndexID to string:
var ToUpload = dtTable.Select().Where( o => !allUploaded.Contains( o[ 0 ].ToString() );
unknowndentified10111 15-Jul-11 0:35am    
yeah! it works.. thank you very much. I have not think about the Convert.. ugh.. I've been debugging this for a whole day.. thanks again..
skv_lviv 15-Jul-11 0:44am    
You're welcome
Try this LINQ query

[EDITED]
C#
// 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)));


For more info look
101 LINQ Samples: Set Operators [ Distinct and Except ][^]
Querying the DataRowView Collection in a DataView[^]
 
Share this answer
 
v5
Comments
unknowndentified10111 15-Jul-11 0:29am    
There was an error:
Could not find an implementation of the query pattern for source type 'System.Data.DataView'. 'Select' not found. Consider explicitly specifying the type of the range variable 'row1'.

And I really cannot specify the columns of dtTable since I put it inside the foreach statement that makes the columns of dtTable different at each loop but the first column is always the IndexID.
Wonde Tadesse 15-Jul-11 0:44am    
Answer updated.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900