Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有关系实体的LINQ连接查询_C#_Linq_Entity Framework Core - Fatal编程技术网

C# 具有关系实体的LINQ连接查询

C# 具有关系实体的LINQ连接查询,c#,linq,entity-framework-core,C#,Linq,Entity Framework Core,我使用的是ef core 2.1,我有以下简化实体,其中一个帐户映射到零个或多个属性对象: public class Account { public int Id { get; set; } public int LongId { get; set; } public List<Attribute> Attributes { get; set; } } public class Attribute { public int Id { get; se

我使用的是ef core 2.1,我有以下简化实体,其中一个
帐户
映射到零个或多个
属性
对象:

public class Account
{
    public int Id { get; set; }
    public int LongId { get; set; }
    public List<Attribute> Attributes { get; set; }
}

public class Attribute
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Value { get; set; }
    public Account Account { get; set; }
}
我得到以下结果集:

id|objType
-----------
cejawq|ext
issokq|ext
cqlpjq|int
mbgzvi|int
wqwlff|ext
iedifh|null
我正在努力将其映射到高效的LINQ,以便生成的SQL远程生成结果集,并将数据发送回我可以投射到等效的匿名类型。我是否需要关心LINQ案例中的父对象?我在
属性.Value
列上没有索引

属性表包含以下数据:

Id|Name   |Value |AccountId
1 |uid    |cejawq|1
2 |objType|ext   |1
3 |uid    |issokq|2
4 |objType|ext   |2
5 |uid    |cqlpjq|3
6 |objType|int   |3
7 |uid    |mbgzvi|4
8 |objType|int   |4
9 |uid    |wqwlff|5
10|objType|ext   |5

由于EF Core不支持与内存序列的联接(但),因此您可以将查询分为两部分—一部分将数据服务器端(
[Attributes
[Attributes
联接)使用内存集合作为过滤器(SQL
通过LINQ
包含
方法),第二个将在内存中对db查询的结果执行左联接:

DbContext db = ...;
var uids = new [] { "cejawq", "issokq", "cqlpjq", "mbgzvi", "wqwlff", "iedifh" };

var dbQuery =
    from attr1 in db.Set<Attribute>()
    where attr1.Name == "uid" && uids.Contains(attr1.Value)
    join attr2 in db.Set<Attribute>()
    on new { AccountId = attr1.Account.Id, Name = "objType" }
    equals new { AccountId = attr2.Account.Id, attr2.Name }
    into attr2Group from attr2 in attr2Group.DefaultIfEmpty() // left outer join
    select new { uid = attr1.Value, objType = attr2.Value };

var query =
    from uid in uids
    join dbResult in dbQuery on uid equals dbResult.uid
    into dbResultGroup from dbResult in dbResultGroup.DefaultIfEmpty() // left outer join
    select new { uid, dbResult?.objType };

var result = query.ToList();

请为您的查询示例发布dbo.Attributes表。我添加了一些示例数据,说明了查询所利用的功能。我在回答中添加了模型和数据上下文类。我在属性类中添加了属性AccountId。我在尝试构建查询时注意到,不合并AccountId属性是一个错误这让我很难做到。我要到早上才能访问完整的数据集,但我计划尽快尝试一下。非常感谢您提供的详细帮助!太棒了,查询立即返回了生产数据的结果集。您帮了我很大的忙。
DbContext db = ...;
var uids = new [] { "cejawq", "issokq", "cqlpjq", "mbgzvi", "wqwlff", "iedifh" };

var dbQuery =
    from attr1 in db.Set<Attribute>()
    where attr1.Name == "uid" && uids.Contains(attr1.Value)
    join attr2 in db.Set<Attribute>()
    on new { AccountId = attr1.Account.Id, Name = "objType" }
    equals new { AccountId = attr2.Account.Id, attr2.Name }
    into attr2Group from attr2 in attr2Group.DefaultIfEmpty() // left outer join
    select new { uid = attr1.Value, objType = attr2.Value };

var query =
    from uid in uids
    join dbResult in dbQuery on uid equals dbResult.uid
    into dbResultGroup from dbResult in dbResultGroup.DefaultIfEmpty() // left outer join
    select new { uid, dbResult?.objType };

var result = query.ToList();