linq用于比较2个表 [英] linq for comparing 2 tables
本文介绍了linq用于比较2个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个字段共用的表。我的问题是公共字段区分大小写(即ID键字段在一个表中有charg11而在另一个表中有CHARG11)
如何在C#中构建linq查询来比较两个字段和让它成真??? Plz帮助我..
谢谢..
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..
推荐答案
Bill和Sascha的评论激励我提供一种可能的解决方案:
Bill's and Sascha's comments inspired me to provide one of possible solutions:
//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.
做类似的事情
var user = UserTable.FirstOrDefault(u=>String.Equals(u.ID, targetString, StringComparison.OrdinalIgnoreCase));
这篇关于linq用于比较2个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文