2011-12-01 59 views
-1

我正在循環瀏覽父記錄(「歐洲」)並更新其名爲「子出版物」的字段及其子記錄。但是子版本在循環和賦值之後爲空?循環(IEnumerable)結果和更新,但沒有更新?

這裏是我的代碼:

foreach (var e in europe) 
{ 
    string child = e.HasChild ?? ""; 
    if (child.Contains("True")) 
    { 
     IEnumerable<Publication> eChildrens = children.OfType<Publication>() 
               .Where(ep => ep.ParentID.Equals(e.PublicationId)); 

     if (eChildrens.Count() > 0) 
     { 
      e.ChildPublication = eChildrens; 
     } 
    } 
}    

member.EuropeMiddleEastAfricaPublication = europe; 

public class Publication 
{ 
    public int PublicationId { get; set; } 
    public int ContentTypeId { get; set; } 
    public string PublicationName { get; set; } 
    public string PublicationFullName { get; set; } 
    public string ShortDescription { get; set; } 
    public string LongDescription { get; set; } 
    public string URL { get; set; } 
    public string CountryId { get; set; } 
    public string LanguageId { get; set; } 
    public string Active { get; set; } 
    public string Subscription { get; set; } 
    public string ClientOnly { get; set; } 
    public string PrintVersion { get; set; } 
    public string EmailVersion { get; set; } 
    public string RegisteredforPrint { get; set; } 
    public string RegisteredforEmail { get; set; } 
    public int ParentID { get; set; } 
    public string HasChild { get; set; } 
    public IEnumerable<Publication> ChildPublication { get; set; } 
} 
+4

您是否確認您至少在指派childPublication的代碼中最終生效? –

回答

0

你應該調試你的程序,並確認你確實進入了你的if並設置屬性。如果你不是,那麼它絕對是空的。但請注意,通過關閉循環變量,您正在做一些危險的事情。

IEnumerable<Publication> eChildrens = 
    children.OfType<Publication>().Where(ep => 
             ep.ParentID.Equals(e.PublicationId)); 

if (eChildrens.Count() > 0) 
{ 
    e.ChildPublication = eChildrens; 
} 

eChildrens是懶惰地評估查詢,並將其是捕獲循環變量e。當你在查詢之外並嘗試使用結果時,除非你有奇怪的期望,否則你的代碼不會做你想做的事。在關閉時,捕獲的是變量,因此當您脫離循環時,您的查詢將始終在查看相同的var e。你將有很多對象看着錯誤的ChildPublication序列。

爲了避免這個問題,可以創建循環中的局部臨時變量並通過調用方法如ToList();

IEnumerable<Publication> eChildrens = 
    children.OfType<Publication>().Where(ep => 
             ep.ParentID.Equals(e.PublicationId)).ToList(); 

if (eChildrens.Count() > 0) 
{ 
    e.ChildPublication = eChildrens; 
} 

關閉了該

var temp = e; // local temporary variable, used below 
IEnumerable<Publication> eChildrens = 
    children.OfType<Publication>().Where(ep => 
             ep.ParentID.Equals(temp.PublicationId)); 

if (eChildrens.Count() > 0) 
{ 
    e.ChildPublication = eChildrens; 
} 

或者交替強制查詢的評價有關此主題的更多信息,請閱讀Eric Lippert的blog entry

+0

感謝您的洞察力,使用列表所需的問題。 – Chaka

-1

您是否嘗試過使用List<Publication>代替或IEnumerable<Publication>。對於簡單的收集處理,我總是有更多的成功。 IEnumerable通常需要一個Enumerator來定義哪一個是更多的開銷。

+1

什麼開銷?你知道列表有IEnumerable (等)作爲基礎接口,因此也有一個枚舉器嗎? –

1

首先,你有eChildren = children,所以我假設孩子在某處傳遞?

,我可能會編寫它像:

foreach (var e in europe) 
{ 
    // .Net 4.0 use: string.IsNullOrWhiteSpace() 
    if (!string.IsNullOrEmpty(e.HadChild) 
     // I prefer IndexOf which allows Culture and IgnoreCase 
     && e.HasChild.IndexOf("True", StringComparison.CurrentCultureIgnoreCase)) 
    { 
    IEnumerable<Publication> eChildrens = 
     children.OfType<Publication>() 
       .Where(ep => ep.ParentID.Equals(e.PublicationId)) 
       .ToList(); //Force the IEnumeration to Enumerate. 

    if (eChildrens.Count() > 0) 
    { 
     e.ChildPublication = eChildrens; 
    } 
    } 
}  
0

這是問題所在。 IEnumerable用戶迭代器針對數據源運行。迭代器返回一個新的對象,而不是對原始對象的引用。

因此,您正在對原始數據的副本進行更改,而不是您的實際數據。如果你想修改你的原始數據,你需要傳遞一個引用並使用該引用。