【发布时间】:2016-11-07 07:12:16
【问题描述】:
我有如下两个数据表,
dtOrigin
RowId Stk ProdName
2 245 ABC
4 144 XYZ
5 122 ADE
dt1
RowId Stk
2 2
4 7
我需要合并这两个数据表以产生以下结果,基本上如果 dt1 中存在 rowid,则需要从 dtOrigin 中减去其库存数量 dt新
RowId Stk ProdName
2 243(245-2) ABC
4 137(144-7) XYZ
5 122 ADE
我可以通过循环来做到这一点,但无论如何都可以在没有循环的情况下做到这一点 谢谢
var JoinResult = (from p in dt1.AsEnumerable()
join t in dt2.AsEnumerable()
on p.Field<string>("RowID") equals t.Field<string>("RowID")
into joinedtables from stuff in joinedtables.DefaultIfEmpty()
select new
{
----------------,
----------------,
Stock = p.Field<Int32>("Stk") - stuff.Field<Int32>("Stk")
}
抛出异常。请指正好吗?
下面是我使用的代码
var JoinResult = (from p in dt.AsEnumerable()
join t in dt2.AsEnumerable()
on p.Field<string>("RowID") equals t.Field<string>("RowID")
into joinedtables from stuff in joinedtables.DefaultIfEmpty()
select new
{
RowID = p.Field<string>("RowID"),
ProdName = p.Field<string>("ProdName"),
STK = p.Field<Int32>("STK") - stuff?.Field<Int32>("STK") ?? 0
}
dtable = LINQResultToDataTable(JoinResult);
public static DataTable LINQResultToDataTable<T>(IEnumerable<T> Linqlist)
{
DataTable dt = new DataTable();
PropertyInfo[] columns = null;
if (Linqlist == null) return dt;
foreach (T Record in Linqlist)
{
if (columns == null)
{
columns = ((Type)Record.GetType()).GetProperties();
foreach (PropertyInfo GetProperty in columns)
{
Type IcolType = GetProperty.PropertyType;
if ((IcolType.IsGenericType) && (IcolType.GetGenericTypeDefinition()
== typeof(Nullable<>)))
{
IcolType = IcolType.GetGenericArguments()[0];
}
dt.Columns.Add(new DataColumn(GetProperty.Name, IcolType));
}
}
DataRow dr = dt.NewRow();
foreach (PropertyInfo p in columns)
{
dr[p.Name] = p.GetValue(Record, null) == null ? DBNull.Value : p.GetValue
(Record, null);
}
dt.Rows.Add(dr);
}
return dt;
}
【问题讨论】:
-
如果你使用的是 linq to sql 那么你可以使用 linq join 表达式
-
它抛出了什么异常?
NullReferenceException?InvalidArgumentException?BetweenKeyboardAndChairException?如果您不提供足够的详细信息,任何人几乎都无法提供帮助。 -
@ZevSpitz,例外是 {"Value cannot be null.\r\nParameter name: row"}
-
如果我执行简单的内连接,它可以工作,但我需要左连接
-
如果行存在于第二个数据表中但不存在于第一个数据表中怎么办?另外,同一个数据表中是否可能有多个具有相同
RowID的记录?