Linq to entities - SQL Query - 其中list包含具有2个属性(或更多)的对象

Dry*_*ods 7 c# linq entity-framework

有以下示例:

 var myIds = db.Table1.Where(x=>x.Prop2 == myFilter).Select(x=>x.Id).ToList();
 var results = db.Table2.Where(x=> myIds.Contains(x.T1)).ToList();
Run Code Online (Sandbox Code Playgroud)

这部分是直截了当的.

但是,现在我面临一个"轻微"的变化,我的"过滤器列表"有2个属性,而不是只有一个:

// NOTE: for stackoverflow simplification I use a basic query to 
// get my "myCombinationObject".
// In reality this is a much more complex case, 
// but the end result is a LIST of objects with two properties.
var myCombinationObject = db.Table3.Where(x=>x.Prop3 == myFilter)
                                   .Select(x=> new { 
                                          Id1 = x.T1, 
                                          Id2 = x.T2
                                    }).ToList();

 var myCombinationObjectId1s = myCombinationObject.Select(x=>xId1).ToList();
 var myCombinationObjectId2s = myCombinationObject.Select(x=>xId2).ToList();

 // step#1 - DB SQL part
 var resultsRaw = db.Tables.Where( x=> 
                     myCombinationObjectId1s.Contains(x.Prop1) 
                  || myCombinationObjectId2s.Contains(x.Prop2))
                .ToList();
//  step#2 - Now in memory side - where I make the final combination filter.
var resultsFiltered = resultsRaw.Where( x=>
            myCombinationObject.Contains( 
                       new {Id1 = x.Prop1, Id2 = x.Prop2 }
            ).ToList();
Run Code Online (Sandbox Code Playgroud)

我的问题:是否有可能在步骤#1中合并步骤#2(在linq中查询实体)?

mr1*_*100 2

我曾经成功地做到了你想要的,但是这非常困难并且需要稍微改变实体模型。您需要一个实体来映射类型

new {Id1 = x.Prop1, Id2 = x.Prop2 }
Run Code Online (Sandbox Code Playgroud)

因此,您需要具有 2 个属性的实体 - Id1 和 Id2。如果你有一个 - 很好,如果没有,那么将这样的实体添加到你的模型中:

public class CombinationObjectTable
{
    public virtual Guid Id1 { get; set; }
    public virtual Guid Id2 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

将其添加到您的模型中:

public DbSet<CombinationObjectTable> CombinationObjectTable { get; set; }
Run Code Online (Sandbox Code Playgroud)

创建新的迁移并将其应用到数据库(数据库现在将具有附加表 CombinationObjectTable)。之后您开始构建查询:

DbSet<CombinationObjectTable> combinationObjectTable = context.Set<CombinationObjectTable>();
StringBuilder wholeQuery = new StringBuilder("DELETE * FROM CombinationObjectTable");
foreach(var obj in myCombinationObject)
{
    wholeQuery.Append(string.Format("INSERT INTO CombinationObjectTable(Id1, Id2) VALUES('{0}', '{1}')", obj.Id1, obj.Id2);
}
wholeQuery.Append(
    db.Tables
        .Where( x=> 
                 myCombinationObjectId1s.Contains(x.Prop1) 
              || myCombinationObjectId2s.Contains(x.Prop2))
        .Where( x=>
           combinationObjectTable.Any(ct => ct.Id1 == x.Id1 && ct.Id2 == x.Id2)
        ).ToString();
    );

 var filteredResults = context.Tables.ExecuteQuery(wholeQuery.ToString());
Run Code Online (Sandbox Code Playgroud)

因此,您的主要查询仍然是用 linq 编写的。如果您不想向数据库添加新表,这也是可以实现的。将新类 CombinationObjectTable 添加到模型中,生成新的迁移来添加它,然后从迁移代码中删除创建该表的代码。之后应用迁移。这样数据库架构不会改变,但 EF 会认为数据库中有 CombinationObjectTable。相反,您需要创建一个临时表来保存数据:

StringBuilder wholeQuery = new StringBuilder("CREATE TABLE #TempCombinationObjectTable(Id1 uniqueidentifies, Id2 uniqueidentifier);");
Run Code Online (Sandbox Code Playgroud)

当您在 linq 查询上调用 ToString 方法时,将 CombinationObjectTable 更改为 #TempCombinationObjectTable:

...
.ToString()
.Replace("CombinationObjectTable", "#TempCombinationObjectTable")
Run Code Online (Sandbox Code Playgroud)

其他值得考虑的事情是使用查询参数在 INSERT 语句中传递值,而不是仅仅将它们包含在自己的查询中 - 这当然也可以通过 EF 实现。该解决方案尚未完全准备好应用,而是提示您可以朝哪个方向寻求该解决方案。