Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have tables with field common in both. My problem is the common fields are case sensitive(i.e. ID key field has "charg11" in one table and "CHARG11" in another table )
How can i frame linq query in C# to compare both fields and make it true??? Plz help me..
Thanks..
Posted
Updated 11-May-15 7:20am
v2
Comments
virusstorm 11-May-15 13:36pm    
How are you fetching the data and what is the data being stored in?
Maciej Los 11-May-15 13:49pm    
What have you tried? Where are you stuck?
BillWoodruff 11-May-15 14:25pm    
Think of a where statement in which: thisobj.ID.ToLower() == otherobj.ID.ToLower()
Maciej Los 11-May-15 15:03pm    
Bill, post it as an answer and i'll upvote it ;)
Sascha Lefèvre 11-May-15 15:17pm    
I assume String.Compare(str1, str1, StringComparison.OrdinalIgnoreCase) would be more performant, I guess it won't create temporary copies of the strings.

Bill's and Sascha's comments inspired me to provide one of possible solutions:

C#
//LinqPad sample
DataTable dt1 = new DataTable();
DataColumn dc = new DataColumn("ID", Type.GetType("System.String"));
dt1.Columns.Add(dc);
for (int i=10; i<30; i++)
{
    dt1.Rows.Add(new Object[]{string.Concat("CHARG", i.ToString())});
}

DataTable dt2 = new DataTable();
dc = new DataColumn("ID", Type.GetType("System.String"));
dt2.Columns.Add(dc);
for (int i=10; i<30; i++)
{
    dt2.Rows.Add(new Object[]{string.Concat("charg", i.ToString())});
}

//cross join uses cartesian algorithm
var qry = from r1 in dt1.AsEnumerable()
    from r2 in dt2.AsEnumerable()
    let comp_res = String.Equals(r1.Field<string>("ID"), r2.Field<string>("ID"), StringComparison.OrdinalIgnoreCase)
    where comp_res
    select new
    {
        r1_ID = r1.Field<string>("ID"),
        r2_ID = r2.Field<string>("ID"),
        comp_result = comp_res
    };
//dump qry object
qry.Dump();


Result:
r1_ID    r2_ID  comp_result
CHARG10 charg10 True 
CHARG11 charg11 True 
CHARG12 charg12 True 
...
CHARG29 charg29 True 


Note: it's just a sample, but it might help you to solve your issue.
 
Share this answer
 
Comments
Sascha Lefèvre 11-May-15 17:06pm    
+5!

"Bill's and Sascha's comments inspired me" - let me be your muse ;-)

comp_result = comp_res ...isn't useful though because it's always true :)

A suggestion: instead of this:
DataColumn dc = new DataColumn("ID", Type.GetType("System.String"));
dt1.Columns.Add(dc);
..you could just write:
dt1.Columns.Add("ID", typeof(string));
Maciej Los 11-May-15 17:14pm    
I say "Yes" twice:
1) comp_result is used to meet OP requirements: How can i frame linq query in C# to compare both fields and make it true. I know, it's stupid...
2) i can use dt1.Columns.Add("ID", typeof(string));, but... i'm inmate of habit ;)
Thank you, Sascha.
Sascha Lefèvre 11-May-15 17:23pm    
1) I think he meant it like "make it work" :)
2) No, you're not! You learned LINQ when it came up, instead of sticking to foreach-loops! ;)
Maciej Los 11-May-15 17:27pm    
;)

Do something like this


C#
var user = UserTable.FirstOrDefault(u=>String.Equals(u.ID, targetString, StringComparison.OrdinalIgnoreCase));
 
Share this answer
 

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