你好,我仍然是 linq 和编程的新手,我正在尝试使用带有 linq 查询的水晶报表制作报告,并将其放入数据表中,我正在使用抛出的函数,但引用的列不在范围内:''。 .
我正在尝试加入 3 张桌子。
这是我从网上找到的一个功能
public DataTable LINQToDataTable<T>(IEnumerable<T> varlist)
{
DataTable dtReturn = new DataTable();
// column names
PropertyInfo[] oProps = null;
if (varlist == null) return dtReturn;
foreach (T rec in varlist)
{
// Use reflection to get property names, to create table, Only first time, others will follow
if (oProps == null)
{
oProps = ((Type)rec.GetType()).GetProperties();
foreach (PropertyInfo pi in oProps)
{
Type colType = pi.PropertyType;
if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()
== typeof(Nullable<>)))
{
colType = colType.GetGenericArguments()[0];
}
dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
}
}
DataRow dr = dtReturn.NewRow();
foreach (PropertyInfo pi in oProps)
{
dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue
(rec, null);
}
dtReturn.Rows.Add(dr);
}
return dtReturn;
}
这是我的 linq
var id = (from u in myDb.TBL_TRANSAKSI_MKN_MNMs
join l in myDb.TBL_DETAIL_TRANSAKSIs on u.ID_NOTA equals l.ID_NOTA
//into g1
join m in myDb.TBL_MKN_MNMs on l.ID_MKN_MNM equals m.ID_MKN_MNM
//into g
group new {u,l,m} by new {u.TGL_TRANSAKSI, m.NAMA_MKN_MNM, m.HARGA_JUAL, l.ID_MKN_MNM, u.USERNAME}
into grp
where grp.Key.TGL_TRANSAKSI.Value.Date.Equals(dateTimePicker1.Value.Date)
select new
{
MakanMinum = grp.Key.NAMA_MKN_MNM,
HargaJual = grp.Key.HARGA_JUAL,
sumStok = grp.Sum(groupedthing => groupedthing.l.ID_MKN_MNM),
Tanggal = grp.Key.TGL_TRANSAKSI,
Jumlah = grp.Key.HARGA_JUAL * grp.Sum(groupedthing => groupedthing.l.ID_MKN_MNM),
Total = grp.Sum(grouptotal => grp.Key.HARGA_JUAL * grp.Sum(groupedthing => groupedthing.l.ID_MKN_MNM)),
Username = grp.Key.USERNAME
});
我有一个 inline foreach (T rec in varlist) 是否有任何简单的查询..?? 因为我很困惑加入 3 张桌子......谢谢你的提前