如何修复“源不包含 DataRows"? [英] How to fix "The source contains no DataRows"?
本文介绍了如何修复“源不包含 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 gives "The source contains no DataRows" when Data tables does not contain Matched Records... How to rectify it..pls give your suggistions
推荐答案
两种方式:
- 在调用
CopyToDataTable
之前检查它是否包含带有 - 使用
dtfff.Clone
创建一个与源表具有相同架构的空数据表,并使用循环从 LINQ 查询中填充它.
Enumerable.Any
的行- 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;
第二种方法:
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.
这篇关于如何修复“源不包含 DataRows"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文