向一个方法发送多列的List <>的正确C#语法是什么?

问题描述 投票:0回答:1

用于接收此列表作为参数的方法的正确语法是什么?

var customList = await db.MyDbTable
    .Select(x => new { x.Id, x.ParentId, x.Title })
    .ToListAsync();
MyMethod(customList);

这不起作用...

private void MyMethod(List<int, int, string> inputList)
{
    // process the input list
    return;
}
c#
1个回答
0
投票

您可以创建一个班级:

public class MyClass
{
    public int Id { get; set; }
    public int ParentId { get; set; }
    public string Title { get; set; }
}

然后不要创建匿名类型,而要使用此类:

var customList = await db.MyDbTable
    .Select(x => new MyClass { x.Id, x.ParentId, x.Title })
    .ToListAsync();

您的方法将变成:

private void MyMethod(List<MyClass> inputList)
{
    // process the input list
    return;
}
© www.soinside.com 2019 - 2024. All rights reserved.