2010-06-17 103 views
0

考慮下面的程序有什麼更好的方法做了以下程序(C#3.0)

private static bool CheckFactorPresent(List<FactorReturn> factorReturnCol) 
{ 
    bool IsPresent = true; 
    StringBuilder sb = new StringBuilder(); 

    //Get the exposure names from Exposure list. 
    //Since this will remain same , so it has been done outside the loop 

    List<string> lstExposureName = (from item in Exposures 
        select item.ExposureName).ToList<string>(); 


    foreach (FactorReturn fr in factorReturnCol) 
    { 
    //Build the factor names from the ReturnCollection dictionary 
    List<string> lstFactorNames = fr.ReturnCollection.Keys.ToList<string>(); 

    //Check if all the Factor Names are present in ExposureName list 
    List<string> result = lstFactorNames.Except(lstExposureName).ToList(); 

    if (result.Count() > 0) 
    { 
     result.ForEach(i => 
     { 
     IsPresent = false; 
     sb.AppendLine("Factor" + i + "is not present for week no: " + fr.WeekNo.ToString());   
     }); 
    } 

    } 
    return IsPresent; 
} 

基本上我檢查是否所有的FactorNames [lstFactorNames]存在於

ExposureNames [lstExposureName]列表通過使用lstFactorNames.Except(lstExposureName).

,然後通過使用所述COUNT()函數(如果計數()> 0),我寫錯誤

消息到字符串構建(SB)

我相信有人能肯定寫一個比呈現一個更好的實現。

我期待着同樣的學習新的東西。

我使用C#3.0和DOTNET框架3.5

感謝

+0

還有,你試過嗎? – 2010-06-17 08:10:30

+1

爲什麼創建這個Stringbuilder而不做任何事情呢? – glenatron 2010-06-17 13:41:33

+0

這是否與最後一次編輯之前問的問題有關? – 2010-06-17 13:43:02

回答

0

節省一些命名慣例的問題,我會說,看起來很好(什麼我可以找出沒有看到的休息代碼或目的,命名約定雖然需要一些工作,但偶爾會出現ntnHungarian,PascalCase,camelCase和abbrv混淆,嘗試僅僅命名本地變量camelCase,事情看起來會好很多祝你好運 - 事情目前看起來不錯!

- 編輯 - 此外,您還可以通過只運行一個簡單的foreach清理末迭代:

... 
foreach (var except in result) 
{ 
    isPresent = false; 
    builder.AppendFormat("Factor{0} is not present for week no: {1}\r\n", except, fr.WeekNo); 
} 
... 
相關問題