c# - 在此上下文中仅支持原始类型或枚举类型。链接到 SQL

标签 c# entity-framework linq-to-sql

我有一个表“Components”,其中包含列“AssemblyGuid”和“PartGuid”。 我有一个组件列表“componentsGuids”

 var componentsGuids = _input.Select(x =>
            new Component
            {
                AssemblyGuid = x.AssemblyGuid,
                PartGuid = x.DetailGuid,
            }).ToList();

如何进行 Linq To Sql 查询以通过复合键加载“componentsGuids”列表中的所有组件条目?

   var componentsDb = Context.Components//.ToList() an error occurs here
            .Where(
                x => componentsGuids.Any(
                    y => y.AssemblyGuid == x.AssemblyGuid
                    && y.PartGuid == x.PartGuid));

最佳答案

也许这可以帮助您找到解决方案。我不确定代码是否像开箱即用的那样工作,但我希望它能使这个想法清晰。在列表中连接您的两个 guid,并使用 Contains 来检查也连接的 guid 对是否存在。

类似的东西应该可以工作:

var componentsGuidsConcat = componentsGuid
    .Select(p => AssemblyGuid.ToString() + PartGuid.ToString())
    .ToList();

var componentsDb = Context.Components
    .Where(p => componentsGuidsConcat.Contains(
        p.AssemblyGuid.ToString() + p.PartGuid.ToString())
    );

关于c# - 在此上下文中仅支持原始类型或枚举类型。链接到 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49813651/

相关文章:

c# - 统一解析嵌套的json

c# - 如果没有安装 Visual Studio,程序将无法启动?

c# - Entity Framework 4.1 没有向 SQL Server Express 数据库添加任何行

linq-to-sql - 如何使用 Linq to SQL 和 Linq to XML 查询数据库列中的 XML 数据?

c# - 没有第三类的 Linq to SQL 多对多关系

c# - 为什么在我的 C# 泛型方法中得到 "error: ... must be a reference type"?

C# - 随机写入文件 - 在第一行之前写入第二行

c# - 将 Null 值插入到 SQL 表中

c# - MySQL Entity Framework 连接器与 OrderBy() 不兼容吗?

c# - DbContext 全局范围与方法级范围