2015-11-04 94 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