简体   繁体   English

这个 LINQ 查询中的连接有什么问题?

[英]What's wrong with the joins in this LINQ query?

I'm trying to replicate the following SQL query in LINQ:我正在尝试在 LINQ 中复制以下 SQL 查询:

SELECT *
FROM Table1 AS D INNER JOIN Table2 AS DV ON D.Table1Id = DV.Table1Id
                 INNER JOIN Table3 AS VT ON DV.Table3Id = VT.Table3Id
                 INNER JOIN Table4 AS C ON DV.CurrencyId = C.CurrencyId
                 INNER JOIN Table5 AS FP ON DV.DVDate BETWEEN FP.StartDate AND FP.EndDate
                 INNER JOIN Table6 AS FX ON DV.CurrencyId = FX.FromCurrencyId AND FX.ToCurrencyId = 'USD' AND FX.FiscalPeriodId = FP.FiscalPeriodId

This is what I have in LINQ:这就是我在 LINQ 中的内容:

from d in db.Table1
join dv in db.Table2 on d.Table1Id equals dv.Table1Id
join vt in db.Table3 on dv.Table3Id equals vt.Table3Id
join c in db.Table4 on dv.CurrencyId equals c.CurrencyId
join fp in db.Table5 on dv.DVDate >= fp.StartDate && dv.DVDate <= fp.EndDate //error on this line
join fx in db.Table6 on dv.CurrencyId equals fx.FromCurrencyId && fx.ToCurrencyId equals "USD" && fx.FiscalPeriodId equals fp.FiscalPeriodId //error also on this line

The last two joins to fp and fx are the problem but it's not clear to me what's wrong, it doesn't seem to like && but there's no and keyword like there is an equals that replaces = . fpfx的最后两个连接是问题,但我不清楚有什么问题,它似乎不喜欢&&但没有and关键字,就像有一个替换=equals一样。

I've removed the select portion from LINQ as it's not relevant to the problem and I'd like to avoid spending more time obfuscating table and field names.我已经从 LINQ 中删除了select部分,因为它与问题无关,我想避免花费更多时间混淆表和字段名称。

"A join clause performs an equijoin. In other words, you can only base matches on the equality of two keys. Other types of comparisons such as "greater than" or "not equals" are not supported. To make clear that all joins are equijoins, the join clause uses the equals keyword instead of the == operator. " “连接子句执行等值连接。换句话说,您只能基于两个键的相等性进行匹配。不支持其他类型的比较,例如“大于”或“不等于”。为了明确所有连接都是equijoins,join 子句使用 equals 关键字而不是 == 运算符。"

reference: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/join-clause参考: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/join-clause

you need to do this in the where clause.您需要在where子句中执行此操作。 Like here:像这儿:

https://stackoverflow.com/a/3547706/3058487 https://stackoverflow.com/a/3547706/3058487

To do a join using composite keys, you need to do something like here:要使用复合键进行连接,您需要执行以下操作:

new { dv.CurrencyId, fp.FiscalPeriodId } equals new { CurrencyId = fx.ToCurrencyId, fx.FiscalPeriodId }

Reference: https://docs.microsoft.com/en-us/dotnet/csharp/linq/join-by-using-composite-keys参考: https://docs.microsoft.com/en-us/dotnet/csharp/linq/join-by-using-composite-keys

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM