用于接收此列表作为参数的方法的正确语法是什么?
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;
}
您可以创建一个班级:
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;
}