[英]C# LINQ INNER JOIN is returning wrong count
我有以下帶有 JOIN 的 sql 但它返回的記錄計數為 50 但是當我將其轉換為 LINQ 時,我沒有得到匹配的計數。 我注意到,當我添加 ON 子句時,visual studio intellisense 下拉列表不顯示第二個表的 ID 屬性。 我想知道這是否是一個問題。
這里是簡單的 SQL
SELECT * FROM Table1 T1 JOIN Table2 T2 ON T1.MyId = T2.MyId WHERE T1.IsCompleted
這是我的 Lamba LINQ 評論,其中 VC Intellisense 無法正常工作。 對於 table2,intellisense 下拉列表,它只顯示 Equals、GetHashCode、GetType 和 ToString。 只需手動輸入 MyId 即可成功構建所有內容,但計數太高。 謝謝
var test = this.myDbContent.Table1
.Join (this.myDbContent.Table2
table1 => table1.MyId,
table2 => table2.MyId,
(table1, table2) => new { table1, table2}
)
.Where (joinedTable => joinedTable.Table1.IsCompleted == 1)
Join 是簡單的 LINQ 操作,但通過 LINQ 查詢可以更好地理解。
var query =
from t1 in myDbContent.Table1
join t2 in myDbContent.table2 on t1.MyId equals t2.MyId
where t1.IsCompleted
select new
{
t1,
t2
};
如果您仍然有不同的結果,可能MyId
是可為空的字段,而 EF 也匹配空值。
可以通過選項配置禁用它:
builder.UseSqlServer(ceConnection, x => x.UseRelationalNulls(true));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.