gpt4 book ai didi

c# - Linq c# - 加入多个条件错误

转载 作者:行者123 更新时间:2023-11-30 17:44:19 24 4
gpt4 key购买 nike

我正在尝试使用 3 个连接条件进行查询。但是我得到一个错误。在 sql server 中,它工作得很好,但是当我尝试将它转换为 linq 时,它给了我一个错误。

您可以在错误和查询下方查看。

查询:

var temp = _context
.FavouriteVol
.Join(_context.Favourites,
fv => new { fv.EntityId, fv.CountryId, fv.EType },
f => new { f.EntityId, f.CountryId, f.EType },
(fv, f) => new { Favourites = f, FavouriteVol = fv })
.Where(u => u.Favourites.userId == userId)
.Select(f => f.Favourites)
.ToList();

注意:EntityId (int)、CountryId (string) 和 EType (int)`。问题出在字符串上。但我还需要对字符串进行过滤,所以知道我该怎么做。

错误:

The type arguments for method 'System.Linq.Queryable.Join(System.Linq.IQueryable, System.Collections.Generic.IEnumerable, System.Linq.Expressions.Expression>, System.Linq.Expressions.Expression>, System.Linq.Expressions.Expression>)' cannot be inferred from the usage.

SQL:

SELECT *
FROM db.FavouriteVol FV
INNER JOIN db.Favourite F On F.EType = FV.EType and F.CountryId = FV.CountryId and F.EType = FV.EType
WHERE F.userId = 5

知道如何解决这个问题吗?

谢谢!!

最佳答案

虽然我不清楚为什么包含 CountryId 的连接会导致此错误,但您可以通过分别匹配 CountryId 来解决此问题:

var temp = _context
.FavouriteVol
.Join(_context.Favourites,
fv => new { fv.EntityId, fv.EType },
f => new { f.EntityId, f.EType },
(fv, f) => new { Favourites = f, FavouriteVol = fv })
.Where(u => u.Favourites.userId == userId
&& u.Favourites.CountryId == u.FavouriteVol.CountryId)
.Select(f => f.Favourites)
.ToList();

关于c# - Linq c# - 加入多个条件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29796007/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com