这与该项目的所有记录有特定的条件的项目的回报 [英] return items that all records related to that items have specific conditions
本文介绍了这与该项目的所有记录有特定的条件的项目的回报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请考虑以下情况:
我有这样一个表格:
ID City Status
--------------------------------------
1 1 1
2 1 1
3 1 1
4 2 1
5 2 0
6 3 1
7 3 1
8 3 1
9 3 1
10 4 3
11 4 1
12 4 0
我要回CITIS这是与该城市的所有记录都有状态= 1
键,如果是记录一个人状态和LT;> 1
这个城市从reslut集中排除。在这scenarion我想回到城市:1,3。
I want to return Citis that all records that related to that city have Status=1
and if one of that records has Status<>1
that city exclude from reslut set. in this scenarion I want to return cities : 1 , 3.
我怎么可以用SQL查询或LINQ查询
How I can do this with Sql Query or LINQ query?
感谢
推荐答案
像这样的东西应该这样做。
Something like this should do it.
LINQ:
var citiesToExclude = context.Table
.Where(t => t.Status != 1)
.Select(t => t.City);
var cities = context.Table
.Where(t => t.Status == 1)
.Where(t => !citiesToExclude.Contains(t.City))
.Select(t => t.City)
.Distinct()
.ToList();
SQL:
SQL:
SELECT City
FROM [Table]
WHERE Status == 1
AND City NOT IN (SELECT City FROM [Table] WHERE Status <> 1)
GROUP BY City
希望这有助于。
这篇关于这与该项目的所有记录有特定的条件的项目的回报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文