2012-03-15 60 views
1

假設我有這樣的定義域類:使這一LINQ投影查詢更好

public class MyDomainItem 
{ 
    public string PropertyA {get;set;} 
    public string PropertyB {get;set;} 
    public string ItemStatus {get;set;} 
    public ClassA ObjectA {get;set;} 
    public ClassB ObjectB {get;set;} 
} 

然後,我有一個LINQ查詢,看起來像這樣:根據我所知道的

var mylist = from a in someList 
       join b in someOtherList on a.Id equals b.Id 
       select new MyDomainItem 
       { 
        PropertyA = a.SomeProperty, 
        PropertyB = b.SomeOtherProperty, 
        ObjectA = a, 
        ObjectB = b 
       } 

,我可以解決ItemStatus有兩種方法:

foreach(var i in mylist) 
{ 
    if (i.ObjectA.YetAnotherProperty == "some criteria") 
    { 
     if (i.ObjectA.NestedObject.NestedProperty == "price is missing") 
     { 
      i.ItemStatus = "bad - category 1"; 
     } 
     else 
     { 
      i.ItemStatus = "bad - category 2"; 
     } 
    } 
    else 
    { 
     i.ItemStatus = "good"; 
    } 
} 

或調用功能,在LINQ QUER解決y:

var mylist = from a in someList 
       join b in someOtherList on a.Id equals b.Id 
       select new MyDomainItem 
       { 
        PropertyA = a.SomeProperty, 
        PropertyB = b.SomeOtherProperty, 
        ObjectA = a, 
        ObjectB = b, 
        ItemStatus = ResolveStatus(a) 
       } 

我腦海中有些東西只是不停地說,一定有更好的辦法。我真的很喜歡做的事情大致是這樣的:

var mylist = from a in someList 
       join b in someOtherList on a.Id equals b.Id 
       select new MyDomainItem 
       { 
        PropertyA = a.SomeProperty, 
        PropertyB = b.SomeOtherProperty, 
        ObjectA = a, 
        ObjectB = b, 
        ItemStatus =()=> 
        { 
         if (a.ObjectA.YetAnotherProperty == "some criteria") 
         { 
          if (a.ObjectA.NestedObject.NestedProperty == 
           "price is missing") 
          { 
           return "bad - category 1"; 
          } 
          else 
          { 
           return "bad - category 2"; 
          } 
         } 
         else 
         { 
          return "good"; 
         } 
        } 
       } 

是否有類似的東西,我可以做什麼?

在此先感謝... !!!

+0

http://codereview.stackexchange.com/ – 2012-03-15 21:22:54

+3

你爲什麼更喜歡上一個版本?使用方法似乎是最合理和可讀的 – BrokenGlass 2012-03-15 21:24:07

+0

匿名方法中的「i」是什麼? – 2012-03-15 21:34:34

回答

1

這樣的事情呢?

select new MyDomainItem 
{ 
    PropertyA = a.SomeProperty, 
    PropertyB = b.SomeOtherProperty, 
    ObjectA = a, 
    ObjectB = b, 
    ItemStatus = (a.YetAnotherProperty == "some criteria") 
        ? (a.NestedObject.NestedProperty == "price is missing" 
         ? "bad - category 1" 
         : "bad - category 2") 
        : "good"; 

雖然我會建議擴展ObjectA與屬性,爲您做這個評估,它在LINQ查詢看起來不太好。如果你沒有來源,你可以使用一個擴展方法

string GetItemStatus(this ObjectA item) { 
    if (item.YetAnotherProperty != "some criteria") { 
     return "good"; 
    } 
    if (item.NestedObject.NestedProperty == "price is missing") { 
     return "bad - category 1"; 
    } 
    return "bad - category 2"; 
} 
+0

關於三元運算符的好主意,它離我想要的更近了一步...... – code4life 2012-03-16 14:27:46

1

你爲什麼不只是創建一個接受兩個屬性和兩個對象,並設置狀態MyDomainItem構造?它將使MyDomainItem成爲一個更健壯的類(狀態有效性不會取決於調用者的良好意願),並且還將解決您的樣式問題。

+0

是的,使用MyDomainItem(a,b)和MyDomainItem.Resolve() – Ray 2012-03-15 21:41:43