Click here to Skip to main content
15,945,344 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I have to compare two datatables using LINQ
C#
// compare if fieldsforDynamicGroup have Already groups

            DataTable merged = (from a in fieldsforDynamicGroup.AsEnumerable()
                                join b in Dtlists.AsEnumerable()
                                on a["FieldName"].ToString() equals           b["ListName"].ToString()
                                into g
                                where g.Count() > 0
                                select a).CopyToDataTable();


but the problem is that i can't directly join a["FieldName"].ToString() equals b["ListName"].ToString()

the values of FieldName column is like "Facebook Link" in A
and ListName is like "All recruits who had Facebook Link change yesterday" in B

How can i join on the basis of wildcards %% ...Is there any performance degradation??
Please help!
Posted
Updated 9-Feb-15 9:33am
v3

You can do it with LINQ, but it's not pretty:
C#
DataTable merged = fieldsforDynamicGroup.AsEnumerable()
    .Where(a => Dtlists.AsEnumerable()
        .Select(b => b.Field<string>("ListName") ?? string.Empty)
        .Any(list => list.Contains(a.Field<string>("FieldName")))
        
        // Or:
        // .Any(list => list.IndexOf(a.Field<string>("FieldName"), StringComparison.CurrentCultureIgnoreCase) != -1)
    )
    .CopyToDataTable();
 
Share this answer
 
v3
Comments
Maciej Los 9-Feb-15 17:05pm    
+5
[no name] 9-Feb-15 18:04pm    
Thanks for solution +5 please tell if there some other method that's better than this
[no name] 10-Feb-15 2:01am    
There is no overload that takes a StringComparison parameter.
Its working now
DataTable merged = fieldsforDynamicGroup.AsEnumerable()
.Where(a => Dtlists.AsEnumerable()
.Select(b => b.Field<string>("ListName") ?? string.Empty)
.Any(list => list.Contains(a.Field<string>("FieldName")))
)
.CopyToDataTable();
Richard Deeming 10-Feb-15 7:28am    
Whoops! That's one of the annoying missing overloads on the string methods.

If you need a case-insensitive comparison, you can use IndexOf instead.

I've updated the answer.
[no name] 10-Feb-15 8:24am    
Thanks Richard for help, it really helped me out :) answer accepted
You can't do it with a Linq Join because a Linq Join absolutely requires that the joined columns are compared for being equal or not.

Looks like you want all records from A where "FieldName" occurs in any "ListName" of B. My first idea would be:
C#
var distinctListNames = Dtlists.AsEnumerable()
          .Select(r => r.Field<string>("ListName").ToUpper()).Distinct();

string joinedListNames = String.Join("$$$", distinctListNames); 

foreach(DataRow row in fieldsforDynamicGroup.Rows)
{
   if(joinedListNames.Contains(row.Field<string>("FieldName").ToUpper()))
   {
      // do something with the row
   }
}

The "$$$" serves as some weird separator so that matches "can't" occur across adjacent ListNames.

Maybe not the most performant of all solutions but the best I could come up with quickly ;)

Cheers!
 
Share this answer
 
v2

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



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