[英]LINQ to SQL error on .Join()
我正在嘗試查詢數據庫並加入兩個表。 我從來沒有使用過Join()這種方式,我在第二個Join()上遇到錯誤:
var adjustments = data.Inventory_ARCHIVEs
.Where(i => i.Location == comboBox3.Text &&
upcCodes.Contains(i.UPCCode) &&
(i.AVtime.Value.Date >= dateTimePicker1.Value.Date &&
i.AVtime.Value.Date <= dateTimePicker1.Value.AddDays(1).Date) &&
(i.BVtime.Value.Date >= dateTimePicker1.Value.Date &&
i.BVtime.Value.Date <= dateTimePicker1.Value.AddDays(1).Date))
.GroupBy(i => new { i.UPCCode })
.Select(i => new
{
ID = i.Max(x => x.ID),
i.Key.UPCCode
})
.Join(data.Inventory_ARCHIVEs, a => a.ID,
b => b.ID, (a, b) => new { a, b })
.Join(data.BQItems, x => new { x.a.UPCCode, x.b.Location },
y => new { y.UPC_Code, y.Location }, (x, y) => new
{
ID = x.a.ID,
UPCCode = x.a.UPCCode,
Date = x.b.BVtime.Value.Date,
Description = y.Description,
BVamount = x.b.BVamount,
AVamount = x.b.AVamount,
Difference = x.b.AVamount - x.b.BVamount,
AverageCost = x.b.AverageCost,
ExtCost = (x.b.AVamount - x.b.BVamount) * x.b.AverageCost
});
xaUPCCode
, xbLocation
, y.UPC_Code
和y.Location
是字符串。
這是錯誤:
方法'System.Linq.Enumerable.Join <TOuter,TInner,TKey,TResult>的類型參數(System.Collections.Generic.IEnumerable <TOuter>,System.Collections.Generic.IEnumerable <TInner>,System.Func <TOuter ,TKey>,System.Func <TInner,TKey>,System.Func <TOuter,TInner,TResult>)'無法從用法中推斷出來。 嘗試顯式指定類型參數。
如果我不通過“位置”列包含連接並只使用“UPCCode”,它可以工作,但是當我按列添加第二個連接時,我收到錯誤
我懷疑這是問題 - 至少有一個問題:
.Join(data.BQItems, x => new { x.a.UPCCode, x.b.Location },
y => new { y.UPC_Code, y.Location },
...)
您正嘗試使用兩種不同的匿名類型作為鍵類型加入。 他們有不同的屬性 - 一個有UPCCode
,另一個有UPC_Code
。 你可能想要:
.Join(data.BQItems, x => new { x.a.UPCCode, x.b.Location },
y => new { UPCCode = y.UPC_Code, y.Location },
...)
或者只是更符合您的屬性名稱,以便在任何地方使用UPCCode
或UPC_Code
,而不是混合使用。
您必須非常關心'equals'子句兩側的數據類型,它們應該具有相同的數據類型,如int和int,或者字符串和字符串。
或者使用lambda表達式,第二個和第三個參數在Join子句中必須是相同的數據類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.