0
你好我仍然是新手在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
});
我已經得到了一致的foreach投擲(T REC在varlist中) 有沒有簡單的查詢.. ??因爲我迷惑加入3個表... 謝謝你提前
我已經改變了你的建議..但它仍然拋出相同的代碼。 –