我正试图将3个实体表(仅具有少量列的填充数据)连接到数据表中
DataTable dttable = new DataTable();
dttable.Columns.Add("billno", typeof(String));
dttable.Columns.Add("date", typeof(DateTime));
dttable.Columns.Add("time", typeof(String));
dttable.Columns.Add("sname", typeof(String));
dttable.Columns.Add("name", typeof(String));
dttable.Columns.Add("qty", typeof(Decimal));
dttable.Columns.Add("rate", typeof(Decimal));
var rows = from mobjbmast in Context.bmasts.AsEnumerable()
join mobjbtran in Context.btrans
on mobjbmast.billno equals mobjbtran.billno
join mobjwaiter in Context.waiters
on mobjbmast.scode equals mobjwaiter.code
where mobjbmast.billno == mbillno
select dttable.LoadDataRow(new object[]
{
mobjbmast.billno,
mobjbmast.date,
mobjbmast.time,
mobjwaiter.name,
mobjbtran.name,
mobjbtran.qty,
mobjbtran.rate
}, false);
期望上面的代码可以填充数据表。但是没有任何结果。调试以上代码后,调试器中将显示“行”变量
无法计算表达式。不支持该操作。未知错误:0x80070057'
所以我尝试了这个:
var rows = from mobjbmast in Context.bmasts.AsEnumerable()
join mobjbtran in Context.btrans.AsEnumerable()
on mobjbmast.billno equals mobjbtran.billno
join mobjwaiter in Context.waiters.AsEnumerable()
on mobjbmast.scode equals mobjwaiter.code
where mobjbmast.billno == mbillno
select new { billno = mobjbmast.billno, date = mobjbmast.date, time = mobjbmast.time, sname = mobjwaiter.name, name = mobjbtran.name, qty = mobjbtran.qty, rate = mobjbtran.rate };
方案没有变化。
通过ToList()
var rows = from mobjbmast in Context.bmasts.ToList()
join mobjbtran in Context.btrans.ToList()
on mobjbmast.billno equals mobjbtran.billno
join mobjwaiter in Context.waiters.ToList()
on mobjbmast.scode equals mobjwaiter.code
where mobjbmast.billno == mbillno
select new { billno = mobjbmast.billno, date = mobjbmast.date, time = mobjbmast.time, sname = mobjwaiter.name, name = mobjbtran.name, qty = mobjbtran.qty, rate = mobjbtran.rate };
这次,错误消失了,但是在调试器中,我看到“枚举未产生结果”消息。
还尝试添加DefaultIfEmpty()
。
我知道我可以将此输出加载到List<T>
中。但是这里需要一个数据表,并且不需要一个类。
如何构造查询以返回适当的IEnumerable以转换为数据表?
答案 0 :(得分:0)
最后,根据一些网站的建议,我像这样解决了它
DataTable dttable = new DataTable();
dttable.Columns.Add("billno", typeof(String));
dttable.Columns.Add("date", typeof(DateTime));
dttable.Columns.Add("time", typeof(String));
dttable.Columns.Add("sname", typeof(String));
dttable.Columns.Add("name", typeof(String));
dttable.Columns.Add("qty", typeof(Decimal));
dttable.Columns.Add("rate", typeof(Decimal));
var rows = from mobjbmast in Context.bmasts.AsEnumerable()
join mobjbtran in odlsContext.btrans
on mobjbmast.billno equals mobjbtran.billno
join mobjwaiter in Context.waiters
on mobjbmast.scode equals mobjwaiter.code
where mobjbmast.billno == mbillno
let billarray = new object[]
{
mobjbmast.billno,
mobjbmast.date,
mobjbmast.time,
mobjwaiter.name,
mobjbtran.name,
mobjbtran.qty,
mobjbtran.rate
}
select billarray;
foreach (var array in rows)
{
dttable.Rows.Add(array);
}