LINQ SELECT中的IF语句包含列

时间:2016-09-13 14:51:09

标签: c#

是否可以在linq Select中包含或排除列?

var numberOfYears = Common.Tool.NumberOfYear;
 var list = users.Select(item => new
        {
            Id = item.Id,
            Name= item.Name,
            City= Item.Address.City.Name,
            STATUS = Item.Status,
            if(numberOfYears == 1)
            {
               Y1 = item.Records.Y1,
            }
            if(numberOfYears == 2)
            {
               Y1 = item.Records.Y1,
               Y2 = item.Records.Y2,
            }
            if(numberOfYears == 3)
            {
               Y1 = item.Records.Y1,
               Y2 = item.Records.Y2,
               Y3 = item.Records.Y3,
            }
        }).ToList();
    }

我的想法是,只有具有值

时才显示Y1,Y2,Y3

3 个答案:

答案 0 :(得分:2)

由于dynamic关键字的优点,您现在需要的是C#。下面是一个例子:

public class MyItem
{
    public string Name { get; set; }
    public int Id { get; set; }
}

static void Main(string[] args)
{
    List<MyItem> items = new List<MyItem>
    {
        new MyItem
        {
            Name ="A",
            Id = 1,
        },
        new MyItem
        {
            Name = "B",
            Id = 2,
        }
    };

    var dynamicItems = items.Select(x => {
        dynamic myValue;
        if (x.Id % 2 == 0)
            myValue = new { Name = x.Name };
        else
            myValue = new { Name = x.Name, Id = x.Id };

        return myValue;
    }).ToList();
}

这将返回动态对象列表。一个有1个属性,一个有2个属性。

答案 1 :(得分:1)

尝试这种方法:

var numberOfYears = Common.Tool.NumberOfYear;
var list = users.Select(item => new
    {
        Id = item.Id,
        Name = item.Name,
        City = Item.Address.City.Name,
        STATUS = Item.Status,
        Y1 = numberOfYears > 0 ? item.Records.Y1 : 0,
        Y2 = numberOfYears > 1 ? item.Records.Y2 : 0,
        Y3 = numberOfYears > 2 ? item.Records.Y3 : 0
    }).ToList();

而不是0,添加您的默认值或null

<强>更新 根据您的评论,唯一的选择是动态。这是动态的例子:

var numberOfYears = 3;
var list = users.Select(x =>
{
    dynamic item = new ExpandoObject();
    item.Id = x.Id;
    item.Name = x.Name;
    item.Status = x.Status;

    var p = item as IDictionary<string, object>;
    var recordsType = x.Records.GetType();
    for (var i = 1; i <= numberOfYears; ++i)
        p["Y" + i] = recordsType.GetProperty("Y" + i).GetValue(x.Records);

    return item;
}).ToList();

答案 2 :(得分:0)

您可以像这样使用ExpandoObject

var data = providers.Select(provider =>
{
    dynamic excelRow = new ExpandoObject();

    excelRow.FirstName = provider.FirstName ?? "";
    excelRow.MiddleName = provider.MiddleName ?? "";
    excelRow.LastName = provider.LastName ?? "";
    
    // Conditionally add columns to the object...
    if (someCondition)
    {
        excelRow.Property1ForCondition = provider.Property1ForCondition;
        excelRow.Property2ForCondition = provider.Property2ForCondition;
    }

    excelRow.DueDate = provider.DueDate ?? null;

    .
    .
    .

    return excelRow;
});

上述代码的另一种形式可以是:

var data = new List<ExpandoObject>();

providers.ForEach(provider =>
{
    dynamic excelRow = new ExpandoObject();

    excelRow.FirstName = provider.FirstName ?? "";
    excelRow.MiddleName = provider.MiddleName ?? "";
    excelRow.LastName = provider.LastName ?? "";
    
    // Conditionally add columns to the object...
    if (someCondition)
    {
        excelRow.Property1ForCondition = provider.Property1ForCondition;
        excelRow.Property2ForCondition = provider.Property2ForCondition;
    }

    excelRow.DueDate = provider.DueDate ?? null;

    .
    .
    .

    data.Add(excelRow);
});