我正在通过linq和lambda表达式对两个集合进行分组。
var result = firstCollection.GroupJoin(secondCollection,
s => s.Id,
p => p.Sid,
(first, second) =>
{
return new { first.Id };
});
这很有效。但我想按照这样的条件选择:
var result = firstCollection.GroupJoin(secondCollection,
s => s.Id,
p => p.Sid,
(first, second) =>
{
if (someCondition)
return new { id = first.Id };
else
return new {
id = first.Id,
name = first.Name };
});
但这会给出错误。我怎么能这样做?
答案 0 :(得分:1)
是的,你可以这样,
var result = firstCollection.GroupJoin(secondCollection,
s => s.Id,
p => p.Sid,
(first, second) =>
{
dynamic foo;
if (someCondition)
foo = new { id = first.Id };
else
foo = new {
id = first.Id,
name = first.Name
};
return foo;
});