如何使用Entity Framework和Linq创建此查询

时间:2015-08-23 20:04:10

标签: c# linq linq-to-entities entity-framework-6 predicatebuilder

在使用LinqKit的LinqPad中,我正在执行以下操作:

var topPredicate = PredicateBuilder.True<LandRecord>();
topPredicate = topPredicate.And(a=>a.InstrumentType == "DEED");
topPredicate = topPredicate.And(a=>a.BookType == "OFFICIAL RECORD");

var subPredicate = PredicateBuilder.True<LandRecord>();
subPredicate = subPredicate.And(a=>a.Parties.Any(b=>b.LastName == "SMITH"));
subPredicate = subPredicate.And(a=>a.Parties.Any(b=>b.FirstName == "John"));

LandRecords.AsExpandable().Include(a=>a.Parties).Where(topPredicate).Where(subPredicate).ToList();

这是它正在创建的SQL:

SELECT 
    [Extent1].[LandRecordID] AS [LandRecordID], 
    [Extent1].[DocumentID] AS [DocumentID], 
    [Extent1].[InstrumentNo] AS [InstrumentNo], 
    [Extent1].[BookType] AS [BookType], 
    [Extent1].[BookNo] AS [BookNo], 
    [Extent1].[PageNo] AS [PageNo], 
    [Extent1].[DateFiled] AS [DateFiled], 
    [Extent1].[DateInstrument] AS [DateInstrument], 
    [Extent1].[InstrumentType] AS [InstrumentType], 
    [Extent1].[MortgageAmount] AS [MortgageAmount]
    FROM [LAND].[LandRecord] AS [Extent1]
    WHERE (N'DEED' = [Extent1].[InstrumentType]) AND (N'OFFICIAL RECORD' = [Extent1].[BookType]) AND ( EXISTS (SELECT 
        1 AS [C1]
        FROM [LAND].[Party] AS [Extent2]
        WHERE ([Extent1].[LandRecordID] = [Extent2].[LandRecordID]) AND (N'SMITH' = [Extent2].[LastName])
    )) AND ( EXISTS (SELECT 
        1 AS [C1]
        FROM [LAND].[Party] AS [Extent3]
        WHERE ([Extent1].[LandRecordID] = [Extent3].[LandRecordID]) AND (N'John' = [Extent3].[FirstName])
    ))

我希望它创建的SQL将是以下内容,其中LastName和FirstName将在同一个exists语句中组合/连接。

在生产中,该子查询可以包含一个或多个条件,每个条件都是startswith,endswith,contains或exact match。所以我需要能够一次手动构建该子查询。

SELECT 
    [Extent1].[LandRecordID] AS [LandRecordID], 
    [Extent1].[DocumentID] AS [DocumentID], 
    [Extent1].[InstrumentNo] AS [InstrumentNo], 
    [Extent1].[BookType] AS [BookType], 
    [Extent1].[BookNo] AS [BookNo], 
    [Extent1].[PageNo] AS [PageNo], 
    [Extent1].[DateFiled] AS [DateFiled], 
    [Extent1].[DateInstrument] AS [DateInstrument], 
    [Extent1].[InstrumentType] AS [InstrumentType], 
    [Extent1].[MortgageAmount] AS [MortgageAmount]
    FROM [LAND].[LandRecord] AS [Extent1]
    WHERE (N'DEED' = [Extent1].[InstrumentType]) AND (N'OFFICIAL RECORD' = [Extent1].[BookType]) AND ( EXISTS (SELECT 
        1 AS [C1]
        FROM [LAND].[Party] AS [Extent2]
        WHERE ([Extent1].[LandRecordID] = [Extent2].[LandRecordID]) AND (N'SMITH' = [Extent2].[LastName] AND (N'John' = [Extent2].[FirstName])
    ))

1 个答案:

答案 0 :(得分:0)

您可以单独构建子谓词,如果它包含任何内容,则将其添加到顶级谓词:

var topPredicate = PredicateBuilder.True<LandRecord>();
topPredicate = topPredicate.And(a=>a.InstrumentType == "DEED");
topPredicate = topPredicate.And(a=>a.BookType == "OFFICIAL RECORD");

var subPredicate = PredicateBuilder.True<Party>();
if (!string.IsNullOrWhiteSpace(firstName)
{
    subPredicate = subPredicate.And(b => b.FirstName == firstName);
}
if (!string.IsNullOrWhiteSpace(lastName)
{
    subPredicate = subPredicate.And(b => b.LastName == lastName);
}

// If the subPredicate's body is still just PredicateBuilder.True<Party>(), ignore it.
if (!(subPredicate.Body is ConstantExpression))
{
    topPredicate = topPredicate.And(lr => lr.Parties.AsQueryable().Any(subPredicate));
}

此处使用了lr.Parties.AsQueryable(),因为Parties可能是ICollection,即IEnumerable。由于IEnumerable.Any接受Func而不是Expression,否则代码将在AsQueryable()之后无法编译。

顺便说一句,我的代码不包含AsExpandable(),因为我使用Universal PredicateBuilder