改变foreach只做一个电话

时间:2016-08-17 19:32:17

标签: c# 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”方法?此致

2 个答案:

答案 0 :(得分:4)

您想使用LINQ。

tickInterval: 5

但你真的不应该使用这么多嵌套的三元ifs(true?“true”:( false?“true”:“false”))。真的很快变得非常丑陋。但如果你这样做,至少要使用括号。

答案 1 :(得分:-1)

foreach(...) embarcaciones.Add(...)替换为Select(...).ToList()

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();

如果您使用的是Visual Studio 2015中的标准C#6,也可以使用null coalescing运算符进行清理:

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();