如何使用通配符比较两个数据表? [英] How to compare two datatables using wildcards?
本文介绍了如何使用通配符比较两个数据表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我必须使用LINQ比较两个数据表
Hi All,
I have to compare two datatables using LINQ
// 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();
但问题是我无法直接加入[FieldName]。ToString()等于b [ListName]。ToString()
值FieldName列的结果类似于A / $
中的Facebook链接,而ListName就像昨天有Facebook链接更改的所有新兵中的B
我如何在通配符%%的基础上加入...是否有任何性能下降?
请帮忙!
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!
推荐答案
你可以用LINQ做它,但它不漂亮:
You can do it with LINQ, but it's not pretty:
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();
你不能用Linq Join来做,因为Linq Join绝对需要比较连接列的存在等于或不等。
看起来你想要来自A的所有记录,其中FieldName出现在B的任何ListName中。我的第一个想法是:
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:
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()))
{
// 对行做点什么
}
}
", distinctListNames); foreach(DataRow row in fieldsforDynamicGroup.Rows) { if(joinedListNames.Contains(row.Field<string>("FieldName").ToUpper())) { // do something with the row } }
The "
这篇关于如何使用通配符比较两个数据表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文