在列表中查找间隙编号

时间:2013-10-14 13:20:11

标签: c# visual-studio-2010 linq tsql

我正在c#,visual studio 2010中编写一个应用程序 我有以下数据

Id  tagNo TermNo
1    1000   2
2    1000   3
3    1000   7
4    1002   1
5    1002   10

如何通过linq或tsql实现以下结果

tagNo   TermNo
1000    1,4,5,6
1002    2,3,4,5,6,7,8,9

谢谢

5 个答案:

答案 0 :(得分:1)

我用linq解决了它,

var tagsList = sourceLists.Select(t => t.TagNo).Distinct().ToList();
foreach (var tagList in tagsList)
{
var terminalList = sourceLists.Where(t => t.TagNo == tagList).Select(t => int.Parse(t.TermNo)).ToList();    
var result = Enumerable.Range(1, terminalList.Max()).Except(terminalList).ToList();
}   

但任何人都可以告诉我是否有可能在TSQL中 谢谢

答案 1 :(得分:1)

我会做以下事情:

var openTags =
    from item in source                           // Take the source items
    group item by item.tagNo into g               // and group them by their tagNo.
    let inUse = g.Select(_ => _.TermNo)           // Find all of the in-use tags
    let max = inUse.Max()                         // and max of that collection.
    let range = Enumerable.Range(1, max - 1)      // Construct the range [1, max)
    select new
    {                               // Select the following
       TagNo = g.Key                // for each group
       TermNo = range.Except(inUse) // find the available tags
    };

答案 2 :(得分:0)

使用oracle做一个提示如下 - (你也可以在TSQL中实现它)

SELECT COLUMN_VALUE 
FROM TABLE(SYS.DBMS_DEBUG_VC2COLL(1,2,3,4,5,6,7,8,9,10))
MINUS
SELECT '4'
FROM DUAL;

这将从提供​​的列表中过滤掉'4'。 (1至10)
同样,您可以过滤掉您的项目(2,3,7,1,10)提供,您需要编写查询

答案 3 :(得分:0)

假设 sourceLists实际上是一个EF实体,以下内容应该在数据库上执行全部

var terminalsByTag = sourceLists.GroupBy(x => x.TagNo)
                                .Select(x => new {
                                    TagNo = x.Key,
                                    Terminals = x.Select(t => Int32.Parse(t.TermNo))
                                });
var result = Enumerable.Range(1, terminalsByTag.Max(g => g.Terminals.Max()).Except(g => g.Terminals).ToList();

答案 4 :(得分:0)

这是您请求(更正)的TSQL解决方案:

declare @t table(Id int, tagNo int, TermNo int)
insert @t values
(1,1000,2), (2,1000,3), (3,1000,7), (4,1002,1), (5,1002,10)

;with a as
(
  select max(TermNo)-1 MaxTermNo, TagNo
  from @t 
  group by TagNo
),
b as
(
  select 1 TermNo, TagNo, MaxTermNo
  from a
  union all
  select TermNo+1, TagNo, MaxTermNo
  from b 
  where TermNo < MaxTermNo
), 
c as
(
  select TermNo, TagNo 
  from b
  except 
  select TermNo, TagNo
  from @t
)
select t.TagNo 
    ,STUFF(( 
        select ',' + cast(TermNo as varchar(9))
        from c t1 
        where t1.TagNo = t.TagNo
        order by TermNo
        for xml path(''), type 
    ).value('.', 'varchar(max)'), 1, 1, '') TermNo 
from c t 
group by t.TagNo 
option (maxrecursion 0) 

结果:

TagNo   TermNo
1000    1,4,5,6
1002    2,3,4,5,6,7,8,9