我不能在连接中的“on”使用两个不同的条件吗?

时间:2012-07-17 15:30:20

标签: linq join

试过这个:

myStructs = from MyObject s in MyObjects
            join c in Categories on s.CategoryID equals c.Item1 && s.Stars equals c.Item2
            select s;

但似乎我不能在加入中写下2个条件?我哪里错了?在SQL上也可以这样做......

1 个答案:

答案 0 :(得分:2)

您需要匿名类型才能加入多个条件/字段:

myStructs = from s in MyObjects
            join c in Categories 
            on new { s.CategoryID, s.Stars }  equals new { CategoryID = c.Item1, Stars = c.Item2 }
            select s;