2016-08-17 40 views
-2

我有一個名單,我打電話給他們的項目的foreach,我想要做的就是使用只需一個電話,而不是每個項目:有我的邏輯變化的foreach做只有一個呼叫

List<Embarcaciones> embarcaciones = new List<Embarcaciones>(); 

foreach (ListItem item in itemsEmbarcaciones) 
{ 
    embarcaciones.Add(new Embarcaciones 
    { 
     Categoria = idioma == "Espanol" ? 
     item["Categoria"] == null ? string.Empty : 
       ((Microsoft.SharePoint.Client.FieldLookupValue)(item["Categoria"])).LookupValue 
     : item["Categoria_x003a_English"] == null ? string.Empty : 
       ((Microsoft.SharePoint.Client.FieldLookupValue)(item["Categoria_x003a_English"])).LookupValue, 

     Title = item["Title"] == null ? string.Empty : item["Title"].ToString(), 
     Imagen = item["Imagen"] == null ? string.Empty : (item["Imagen"] as FieldUrlValue).Url, 
     Enlace = item["Enlace"] == null ? string.Empty : item["Enlace"].ToString(), 
     Especificaciones = item["Especificaciones"] == null ? string.Empty : item["Especificaciones"].ToString(), 
     Specifications = item["Specifications"] == null ? string.Empty : item["Specifications"].ToString() 
    }); 
} 

result.Embarcaciones = categoria.Contains("\"") ? 

embarcaciones.Where(x => x.Categoria.ToLower().Contains(categoria.ToLower())).ToList() : 
embarcaciones.Where(x => x.Categoria.ToLower().Equals(categoria.ToLower())).ToList(); 

哪有我改變我的方法只調用一次而不是「foreach」方法?問候

回答

4

你想使用LINQ。

List<Embarcaciones> embarcaciones = itemsEmbarcaciones.Select(x => new Embarcaciones() { 
    Categoria = (idioma == "Espanol" ? (x["Categoria"] == null ? ... : ...) : ...), 
    Title = ... 
    ... 
}).ToList(); 

但你真的不應該用這麼多的嵌套三元IFS被(真正的 「真實」:?(假 「真」: 「假」))。它變得真的很難看。但是,如果你這樣做,至少用括號。

+0

我preder使用新的方法來檢查,如果空'私人字符串CheckNull(列表項項) { \t退貨項目== NULL? string.Empty:item.ToString(); }' – Marusyk

+0

我更進一步,使其成爲擴展方法。使用擴展方法的好處是,由於它們轉化爲靜態調用,所以可以在空值上調用擴展方法。 – jeromeyers

+0

我嘗試,但我收到一個NotSupportedException異常 – Jesus

-1

Select(...).ToList()更換foreach(...) embarcaciones.Add(...)

var embarcaciones = itemsEmbarcaciones.Select(item => new Embarcaciones 
    { 
     Categoria = idioma == "Espanol" ? 
     item["Categoria"] == null ? string.Empty : 
       ((Microsoft.SharePoint.Client.FieldLookupValue)(item["Categoria"])).LookupValue 
     : item["Categoria_x003a_English"] == null ? string.Empty : 
       ((Microsoft.SharePoint.Client.FieldLookupValue)(item["Categoria_x003a_English"])).LookupValue, 

     Title = item["Title"] == null ? string.Empty : item["Title"].ToString(), 
     Imagen = item["Imagen"] == null ? string.Empty : (item["Imagen"] as FieldUrlValue).Url, 
     Enlace = item["Enlace"] == null ? string.Empty : item["Enlace"].ToString(), 
     Especificaciones = item["Especificaciones"] == null ? string.Empty : item["Especificaciones"].ToString(), 
     Specifications = item["Specifications"] == null ? string.Empty : item["Specifications"].ToString() 
    }).ToList(); 

您還可以清理使用null coalescing運營商,如果你使用的是C#6,這是在Visual Studio 2015年標準:

using Microsoft.SharePoint.Client; 

//... 

var embarcaciones = itemsEmbarcaciones.Select(item => new Embarcaciones 
    { 
     Categoria = idioma == "Espanol" 
         ? (item["Categoria"] as FieldLookupValue)?.LookupValue ?? string.Empty 
         : (item["Categoria_x003a_English"] as FieldLookupValue)?.LookupValue ?? string.Empty, 

     Title = item["Title"] as string ?? string.Empty, 
     Imagen = (item["Imagen"] as FieldUrlValue)?.Url ?? string.Empty, 
     Enlace = item["Enlace"] as string ?? string.Empty, 
     Especificaciones = item["Especificaciones"] as string ?? string.Empty, 
     Specifications = item["Specifications"] as string ?? string.Empty 
    }).ToList(); 
+0

空合併運算符只支持C#6.0,你的答案是一樣的@jeromeyers – Marusyk

+0

C#6不是在Visual Studio 2015年的標準!例如,我可以使用VS2015和.Net Framework 2.0。只有在.Net框架4.6或.Net核心建造支持C#6.0 – Marusyk

+0

@MegaTron IDGAF你已經更改了設置的項目。 C#6是開箱即用的新C#項目的默認值,對我而言,「標準」意味着什麼。 – RoadieRich