C# 如何将匿名键入的列表转换为列表<;T>;?

C# 如何将匿名键入的列表转换为列表<;T>;?,c#,.net,linq,generics,anonymous-types,C#,.net,Linq,Generics,Anonymous Types,这个简单的Linq查询: from c in mycontext.Customers join o in mycontext.Orders on c.CustomerId equals o.CustomerId where o.Status == 1 select new {c, o} 将导致 List<{c:Customer, o:Order}> 列表 调用ToList()后 将匿名输入的列表转换为客户列表(list)的最简单方法是什么 编辑:我需要一个额外条件的订单,我已经

这个简单的Linq查询:

from c in mycontext.Customers
join o in mycontext.Orders on c.CustomerId equals o.CustomerId
where o.Status == 1
select new {c, o}
将导致

List<{c:Customer, o:Order}>
列表
调用
ToList()

将匿名输入的列表转换为客户列表(
list
)的最简单方法是什么


编辑:我需要一个额外条件的订单,我已经改变了我原来的问题。

非常基本的方法,正如你明确地问到的“转换这个匿名键入[…]的最简单方法是什么?”

var anonymouseneumerable=来自mycontext.Customers中的c
在mycontext中加入o。c.CustomerId上的订单等于o.CustomerId
选择新的
{
C
o
};
var typedEnumerable=anonymousList.Select(item=>item.c).Distinct();//referenceCheck或提供IEqualityComparer实现
也许你可以给我们一些更多的信息和你想要实现的目标

result.Select(o => o.Customer).ToList();

这就是你的意思吗?

你需要这两种属性吗?如果是这样,请逐步浏览列表并实例化每个游标编辑器

差不多

 List<Customer> customers = new List<Customer>();
 foreach(item in linqQuery.ToList())
 {

     customers.Add(item.c);
     //do something with the Order here...
 }
列出客户=新建列表();
foreach(linqQuery.ToList()中的项)
{
添加(c项);
//按这里的命令做点什么。。。
}

为什么不直接使用
.ToList()

不要选择订单-加入后您不需要它们

List<Customer> custList =  (from c in mycontext.Customers
    join o in mycontext.Orders on c.CustomerId equals o.CustomerId
    where o.Status == 1
    select c).ToList<Customer>();
List custList=(来自mycontext.Customers中的c)
在mycontext中加入o。c.CustomerId上的订单等于o.CustomerId
其中o.Status==1
选择c.ToList();

这将是我对解决方案(包括拼写错误等)的排序:)

我希望尽可能简单地将Linq查询的结果转换为列表。对于此列表,我给出了答案。。。如果你想要一个更一般的答案,可能还有另一个解决方案:)你到底需要订单还是?如果没有,您可以直接“选择c”而不是“选择新{c,o}”,如果您只需要客户,为什么要加入订单?这将返回每个客户
X
次,其中
X
是与每个客户关联的订单数。你的答案没有错,但也许他想要的是不同的客户,他最好知道这个例子不能做到这一点。很好。问题是,为什么会有订单连接?无论如何,如果需要,您仍然可以调用.Distinct()。这不是最简单的方法,尽管您不需要
.ToList()
。实际上,你不需要4行就可以做到,但这是一个选择的问题。
List<Customer> custList =  (from c in mycontext.Customers
    join o in mycontext.Orders on c.CustomerId equals o.CustomerId
    where o.Status == 1
    select c).ToList<Customer>();
var ledger = from c in mycontext.Customers
                 join o in mycontext.Orders on c.CustomerId equals o.CustomerId
                 where o.Status == 1
                 select new {c, o};

var customers = (from row in ledger select row.Customer).Distinct().ToList();