我有这样一个表:
position title
---------------------------
1 "t1"
2 "t1"
3 "t2"
4 "t1"
5 "t2"
我想过滤"t1"标题,但对于当前位置和下一个位置有1个差值的位置。根据这个,如果我想要第一个"t1"的结果应该是[1,2]。
我怎么能写这个查询使用linq 2 sql?
听起来你想要一个自连接:
var query = from item1 in db.Items.Where(x => x.title == "t1")
from item2 in db.Items.Where(x => x.title == "t1")
where item1.position + 1 == item2.position
select item1; // Adjust however you want, e.g. new { item1, item2 }
现在我不知道这是否会在SQL中实际工作…但从逻辑上讲,这就是你想要的。