2015-11-04 111 views
2

我试图替换我的大,丑陋的查询;虽然丑它的工作原理是期望: -LINQ查询与包含语句中的Where子句

using (var ctx = new Data.Model.xxxTrackingEntities()) 
{ 
    var result = ctx.Offenders 
     .Join(ctx.Fees, o => o.OffenderId, f => f.OffenderId, 
     (o, f) => new { Offenders = o, Fees = f }) 
     .Join(ctx.ViolationOffenders, o => o.Fees.ViolationId, vo => vo.ViolationId, 
     (o, vo) => new { Offenders = o, ViolationOffenders = vo }) 
     .Join(ctx.Violations, v => v.ViolationOffenders.ViolationId, vo => vo.ViolationId, 
     (v, vo) => new { Violations = v, ViolationOffenders = vo }) 
     .Where(o => o.Violations.Offenders.Offenders.YouthNumber != "") 
     .ToList(); 

    gvwData.DataSource = result; 
} 

具有以下LINQ查询: -

var result = ctx.Offenders 
     .Include(o => o.Fees.Where(f => f.Amount != null)) 
     .Include(o => o.ViolationOffenders) 
     .Include(o => o.ViolationOffenders.Select(of => of.Violation)) 
     .Where(o => o.YouthNumber != "" && o.FirstName != "") 
     .ToList(); 

我吹起来的查询二号线......一旦我添加了WHERE子句。 .. o => o.Fees.Where(f=> f.Amount != null)

该错误消息我得到...

The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.

此外,我试图写M个Ÿ查询为: -

var result = ctx.Offenders 
     .Include(o => o.Fees) 
     .Include(o => o.ViolationOffenders) 
     .Include(o => o.ViolationOffenders.Select(of => of.Violation)) 
     .Where(o => o.YouthNumber != "" && o.FirstName != "" && o.Fees.Where(f=> f.Amount != null)) 
     .ToList(); 

但后来我得到以下错误: -

Operator '&&' cannot be applied to operands of type 'bool' and 'System.Collections.Generic.IEnumerable

我知道这个概念是对的,但我需要的语法帮助。

+0

什么'o.Fees.Where(F => f.Amount!= NULL)'应该是。你对它有什么期望? –

+0

我希望我的结果变量中的1个项目每个Fee项目 – Philo

回答

5

你不能有一个WhereWhere内,但您可以使用Any这将返回一个布尔

var result = ctx.Offenders 
    .Include(o => o.Fees) 
    .Include(o => o.ViolationOffenders) 
    .Include(o => o.ViolationOffenders.Select(of => of.Violation)) 
    .Where(o => o.YouthNumber != "" && o.FirstName != "" 
     && o.Fees.Any(f=> f.Amount != null)) // here 
    .ToList(); 
+0

甜。查询起作用!但是现在我意识到我将我的费用作为一个集合......对于一个结果项目,可能会有多个费用项目。我想要费用和不同.. 1费用项目在我的变量结果中的每1项目.. – Philo