如何修复"源不包含任何数据行"? [英] How to fix "The source contains no DataRows"?
本文介绍了如何修复"源不包含任何数据行"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在这里,我想找到匹配的记录从两个数据表。代码
Here i want to find the Matched Records From Two data tables. the code is
public DataTable textfiltering(DataTable dtfff, DataTable dtff)
{
DataTable ds = (DataTable)Session["maintxt"];
DataTable dts = (DataTable)Session["sectxt"];
dtfff = ds;
dtff = dts;
DataTable dtMerged = (from a in dtfff.AsEnumerable()
join b in dtff.AsEnumerable()
on a["contacts"].ToString() equals b["contacts"].ToString()
into g
where g.Count()>0
select a).CopyToDataTable();
return dtMerged;
}
它给源不包含任何数据行,当数据表不包含匹配的记录...
如何纠正it..pls给你suggistions
it gives "The source contains no DataRows" when Data tables does not contain Matched Records... How to rectify it..pls give your suggistions
推荐答案
有两种方式:
- 要么检查它是否包含与行
Enumerable.Any
你打电话之前CopyToDataTable
- 使用
dtfff.Clone
以创建具有相同的架构作为一个空的DataTable源表,并使用一个循环从LINQ查询填充
- either check if it contains rows with
Enumerable.Any
before you callCopyToDataTable
- use
dtfff.Clone
to create an empty DataTable with the same schema as the source table and use a loop to fill it from the LINQ query.
第一种方法:
var rows = from a in dtfff.AsEnumerable()
join b in dtff.AsEnumerable()
on a["contacts"].ToString() equals b["contacts"].ToString()
into g
where g.Count() > 0
select a;
DataTable merged;
if (rows.Any())
merged = rows.CopyToDataTable();
else
merged = dtfff.Clone();
return merged;
第二条本办法:
Second approach:
DataTable merged = dtfff.Clone();
foreach (DataRow sourceRow in rows)
{
merged.ImportRow(sourceRow); // or add all fields manually
}
return merged;
我更喜欢第二种方法,因为它只需要一次执行查询。
I prefer the second approach since it only needs to execute the query once.
这篇关于如何修复"源不包含任何数据行"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文