How to query a dictionary property in a LINQ expression using Entity Framework in .Net 7.0?

问题描述 投票:0回答:0

我正在尝试使用 .Net 7.0 中的实体框架查询相关实体的字典属性。我有两个表,“位置”和“本地化”。

在我的表 Positions 中,有一个名为“TitleId”的字段与表“Localizations”相关。在 Localization 表中有一个 ID 和一个字段“Values”,里面有一个 JSON,如下所示: {"de": "Beispiel auf Deutsch", "fr": "Example en français", "en" : "Example in English"}

现在我想在 Positions 表的级别进行搜索,以查明标题中是否存在术语,这与使用哪种语言无关。

这是我试过的代码:

 var positions = _dataContext.Positions
            .Include(p => p.Title)
            .Where(p => p.Title.Values.ContainsValue(query))
            .AsAsyncEnumerable();

但是我有以下错误:

The LINQ expression 'DbSet<Position>()\r\n    .LeftJoin(\r\n        inner: DbSet<Localization>(), \r\n        outerKeySelector: p => EF.Property<ulong?>(p, \"TitleId\"), \r\n        innerKeySelector: l => EF.Property<ulong?>(l, \"Id\"), \r\n        resultSelector: (o, i) => new TransparentIdentifier<Position, Localization>(\r\n            Outer = o, \r\n            Inner = i\r\n        ))\r\n    .Where(p => p.Inner.Values.ContainsValue(__query_0))' could not be translated. Additional information: Translation of method 'System.Collections.Generic.Dictionary<string, string>.ContainsValue' failed. If this method can be mapped to your custom function, see https://go.microsoft.com/fwlink/?linkid=2132413 for more information. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

实体类如下: 对于表 Positions,我有一个带有此字段的类 Position:

public Localization Title { get; set;}

对于表 Localizations,我有一个像这样的 Localization 类:

public ulong Id { get; set; }
public Dictionary<string, string> Values { get; set; } = new();

我正在使用 .Net 7.0 和 EntityFramework 6.4.4

c# .net entity-framework linq
© www.soinside.com 2019 - 2024. All rights reserved.