2009-10-28 61 views
2

誰能幫助?我堅持了LINQ查詢我的LINQ查詢..幫助與子查詢

基本上我有一個標準的LINQ查詢,返回場,1場(保險)實際上是另一個LINQ查詢,像這樣

// original from this in etc not included to keep msg short> 
    select new Models.Custom.House.Insurance() 
       { 
        Id = v.IdHouse, 
        Insurances = from gt in GroupHouseTariffs 
           join i in InsuranceGroup 
            on new { gt.IdTariff, gt.IdGroup} 
             equals 
             new { i.IdTariff, i.IdGroup} 
           select new 
             { 
              InsuranceId = i.Id, 
              Price = i.Price 
             } 

基本保險注入從Models.Custom.House保險性質,它的作品,因爲我可以看到它在我調試...我有4條記錄..保險是這樣定義的保險房子基本上是另一個小類的Iqueryable ..

 public IQueryable<Insurance> Insurances { get; set;} 

所以我試着寫一個擴展方法,像這樣

public static IQueryable<House> WithInsuranceId(this IQueryable<House> qry, int insuranceId) 
    { 
     return from h in qry 
       where h.Insurance .. // BUT here is the problem i don't see my properties, I see plenty of linq methods 

    } 

我應該能夠看到insuranceId和做到這一點,不是嗎?

return from h in qry 
       where h.Insurance.InsuranceId == 1; 

這裏是類(其非常小)

public class Insurance 
    { 
     public int? InsuranceId { get; set; } 
     public float? price{ get; set; } 
    } 

也許有某種特殊的lambda的我需要了解:-)?

任何幫助真的很感激,謝謝。

+0

編譯時錯誤做,如果你居然說'你得到什麼地方h.Insurance.InsuranceId == 1;'? – Ecyrb 2009-10-28 13:08:21

+0

那麼實際上編譯器之前,我看到它在紅色,但如果我編譯 「System.Linq.IQueryable 」不包含「InsuranceId」,沒有擴展方法的定義「InsuranceId」接受類型'System.Linq.IQueryable '的第一個參數可以被發現 – 2009-10-28 13:28:32

回答

2

您發佈的樣本中是否存在拼寫錯誤?

我注意到以下幾點:

// No object name specified, Price has a capital P 
select new 
{ 
    InsuranceId = i.Id, 
    Price = i.Price 
} 

// Price has a small p 
public class Insurance 
{ 
    public int? InsuranceId { get; set; } 
    public float? price{ get; set; } 
} 

現在,您的實際查詢看起來我錯了也是如此。

House.Insurance有一個名爲ID的屬性和一個名爲Insurances的屬性,它是一個集合。然而,你的查詢狀態:

return from h in qry 
      where h.Insurance.InsuranceId == 1; 

你的意思是:

return from h in qry 
      where h.Insurance.Id == 1 select h; 

或者:

return from h in qry 
      where h.Insurance.Insurances.Contains(i => i.InsuranceID ==1) select h; 
+0

是錯別字:-) ooops ...但就是這樣! ..包含..作品一個款待謝謝你 – 2009-10-29 19:51:34