C# 数据更改时SQL选择
我希望在SQL Select中检索整数值,但仅当数据发生更改时,例如: 表数据:C# 数据更改时SQL选择,c#,sql,entity-framework,C#,Sql,Entity Framework,我希望在SQL Select中检索整数值,但仅当数据发生更改时,例如: 表数据: 50 50 50 52 50 30 30 35 30 30 60 65 60 60 现在我想得到以下数据: 50 52 50 30 35 30 60 65 60 执行不同的查询时,这对我不起作用,因为它将检索: 50 52 30 35 60 65 有什么想法吗 我正在使用Entity Framework和C,因此建议使用它们也将不胜感激 谢谢。将结果获取到dbResults,然后执行以下操作 var resu
50 50 50 52 50 30 30 35 30 30 60 65 60 60
现在我想得到以下数据:
50 52 50 30 35 30 60 65 60
执行不同的查询时,这对我不起作用,因为它将检索:
50 52 30 35 60 65
有什么想法吗
我正在使用Entity Framework和C,因此建议使用它们也将不胜感激
谢谢。将结果获取到dbResults,然后执行以下操作
var results = new List<int>();
foreach (var element in dbResults)
{
if(!results.Any() || results.Last() != element)
{
results.Add(element);
}
}
int结果将列出,且不连续重复
您可以在上检查它。该方法原则上类似于@wudzik的方法,但它不是每次都检查结果列表,而是将最后一个int存储在变量中,并根据该值进行检查
var result = new List<int>();
int? previous;
foreach (var number in data)
{
if(!previous.HasValue || number != previous.Value){
{
result.Add(number);
previous = number;
}
}
return result
您需要为这个表编写游标如果您想在SQL中这样做,您应该使用ROW_NUMBER和OVER+1来教我一些新东西
List<int> list=...;
var result=Enumerable.Range(0, list.Count- 1)
.Where(x=> x== list.Count-1 || list[x]!=list[x+1])
.Select(x=>list[x]);
WITH cte as (--cte is your test data
SELECT
1 id,
50 value UNION SELECT
2,
50 UNION SELECT
3,
50 UNION SELECT
4,
52 UNION SELECT
5,
50 UNION SELECT
6,
30 UNION SELECT
7,
30 UNION SELECT
8,
35 UNION SELECT
9,
30 UNION SELECT
10,
30 UNION SELECT
11,
60 UNION SELECT
12,
65 UNION SELECT
13,
60 UNION SELECT
14,
60
),
temp AS --temp numbers the rows
( SELECT
id,
value,
ROW_NUMBER() OVER (ORDER BY id) rowno
FROM cte
)
SELECT
t2.value
FROM temp t1
INNER JOIN temp t2
ON t1.rowno = t2.rowno - 1 --join to the next row using rownumber
AND t1.value <> t2.value --but only return different values