LINQ加入,如何施加条件

时间:2017-11-02 12:37:12

标签: c# linq

假设我们有两个列表(FirstSecond

Id  Value
1   A
2   B
3   C

Id  NewValue
1   a10
1   a21
1   a61
2   b71
2   b79
3   c40
3   c48
3   c55

我想执行联接查询

from p in First
join c in Second on p.Id equals c.Id
select new { p.Value, c.NewValue }

但是我也想在NewValue的数字部分施加条件,选择大于40的条件。只有a61,然后是b71,依此类推。 如何修改我的连接查询?

1 个答案:

答案 0 :(得分:4)

您不能在join条件下执行此操作,但添加where子句:

from p in First
join c in Second on p.Id equals c.Id
where int.Parse(c.NewValue.Substring(1)) > 40
select new { p.Value, c.NewValue }

请注意,您可能希望使用int.TryParse并验证解析是否成功