ich zwei Datentabellen wie unten habe,Zusammenführen von zwei Datentabellen mit Check C#
dtOrigin
RowId Stk ProdName
2 245 ABC
4 144 XYZ
5 122 ADE
dt1
RowId Stk
2 2
4 7
Ich brauche diese zwei Datentabellen zu fusionieren unten Ergebnis zu erzielen, im Grunde, wenn Rowid in DT1 vorhanden ist, müssen seinen Lagerbestand auf minus von dtOrigin dtNew
RowId Stk ProdName
2 243(245-2) ABC
4 137(144-7) XYZ
5 122 ADE
ich bin in der Lage diese mit Schleife zu tun, aber ist es trotzdem diese Dank ohne Schleife zu tun
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")
}
Wurf Ausnahme. Kannst du bitte korrigieren?
Unten finden Sie den Code ich dies mit
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;
}
verwenden Wenn Sie Linq verwenden, um SQL dann können Sie verwenden linq Join Ausdruck –
Welche Ausnahme wirft es? 'NullReferenceException'? 'InvalidArgumentException'? 'BetweenKeyboardAndChairException'? Es ist fast unmöglich für jemanden zu helfen, wenn Sie nicht genug Details zur Verfügung stellen. –
@ZevSpitz, die Ausnahme ist {"Wert kann nicht Null sein. \ R \ nParametername: row"} –