Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 合并多个表中的值_Asp.net_Sql_Linq_Join - Fatal编程技术网

Asp.net 合并多个表中的值

Asp.net 合并多个表中的值,asp.net,sql,linq,join,Asp.net,Sql,Linq,Join,我有一个应用程序,它有两个表的数据分布 有一个主表main,其中包含列-Id、Name、Type。 现在有了一个子主表,该表有maindfk、StartDate、Enddate和city列 这是一个1对多的关系,每个main可以在submin中有多个条目 现在我想显示Main.Id列,City列,以逗号分隔,从submain主项的各个行,从submain主项的开始日期的最小值和从submain的结束日期的最大值 我曾想过要有一个功能,但那会让事情变得缓慢,因为会有10万条记录。还有其他的方法吗。

我有一个应用程序,它有两个表的数据分布

有一个主表main,其中包含列-Id、Name、Type。 现在有了一个子主表,该表有maindfk、StartDate、Enddate和city列 这是一个1对多的关系,每个main可以在submin中有多个条目

现在我想显示Main.Id列,City列,以逗号分隔,从submain主项的各个行,从submain主项的开始日期的最小值和从submain的结束日期的最大值


我曾想过要有一个功能,但那会让事情变得缓慢,因为会有10万条记录。还有其他的方法吗。顺便说一句,应用程序在asp.net中。我们可以使用sql查询或类似linq的东西吗?

这是我不知道的,但首先我建议您在sql中创建一个用户定义的函数,以创建接受@mainid的城市逗号分隔列表字符串,然后执行以下操作:

DECLARE @listStr VARCHAR(MAX)
SELECT @listStr = COALESCE(@listStr+',' , '') + city
FROM submain
WHERE mainid = @mainid
。。。然后返回@listStr,它现在是一个逗号分隔的城市列表。假设您调用函数maindcitystringget

然后,对于最终结果,您只需执行以下操作

select cts.mainid,
       cts.cities,
       sts.minstartdate,
       sts.maxenddate
  from ( select distinct mainid,
                         dbo.MainIDCityStringGet(mainid) as 'cities'
           from submain) as cts
         join
       ( select mainid,
                min(startdate) as 'minstartdate',
                max(enddate)   as 'maxenddate'
           from submain
          group by mainid ) as sts on sts.mainid = cts.mainid
 where startdate <is what you want it to be>
   and enddate   <is what you want it to be>
根据您希望按startdate和enddate进行筛选的确切方式,您可能需要在每个子查询中放置where筛选器,然后在联接的第二个子查询中使用HAVING grouped筛选器。您没有明确说明过滤器的性质

我希望这有帮助


这当然会在存储过程中。可能需要一些调试

创建存储过程的另一种方法是在客户端执行复杂的操作。未经测试:

var result = (from main in context.Main
              join sub in context.SubMain on main.Id equals sub.MainId into subs
              let StartDate = subs.Min(s => s.StartDate)
              let EndDate = subs.Max(s => s.EndDate)
              let Cities = subs.Select(s => s.City).Distinct()
              select new { main.Id, main.Name, main.Type, StartDate, EndDate, Cities })
             .ToList()
             .Select(x => new
                          {
                              x.Id,
                              x.Name,
                              x.Type,
                              x.StartDate,
                              x.EndDate,
                              Cities = string.Join(", ", x.Cities.ToArray())
                          })
             .ToList();

我不确定这在其他SQL实现中的支持程度如何,但如果您有SQL Server,这对这种类型的场景非常有用

作为免责声明,我想补充一点,我不是这项技术的创始人。但当我遇到这个问题时,我立刻想到了这个问题

例如:

要一张桌子

Item ID       Item Value       Item Text
-----------  ----------------- ---------------
1             2                 A 
1             2                 B
1             6                 C
2             2                 D
2             4                 A
3             7                 B
3             1                 D
如果您想要以下输出,字符串将连接起来,值将求和

Item ID      Item Value        Item Text
-----------  ----------------- ---------------
1            10                 A, B, C 
2            6                  D, A
3            8                  B, D
以下内容避免了多语句循环解决方案:

if object_id('Items') is not null
  drop table Items
go

create table Items
     ( ItemId    int identity(1,1),
       ItemNo    int not null,
       ItemValue int not null,
       ItemDesc  nvarchar(500) )

insert Items
     ( ItemNo,
       ItemValue,
       ItemDesc )
values ( 1, 2, 'A'),
       ( 1, 2, 'B'),
       ( 1, 6, 'C'),
       ( 2, 2, 'D'),
       ( 2, 4, 'A'),
       ( 3, 7, 'B'),
       ( 3, 1, 'D')

select it1.ItemNo,
       sum(it1.ItemValue) as ItemValues,
       stuff((select ', ' + it2.ItemDesc  --// Stuff is just used to remove the first 2 characters, instead of a substring.
                from Items it2 with (nolock)
               where it1.ItemNo = it2.ItemNo
                 for xml path(''), type).value('.','varchar(max)'), 1, 2, '') as ItemDescs --// Does the actual concatenation..  
  from Items it1 with (nolock)
 group by it1.ItemNo

因此,您所需要的只是select中的一个子查询,它检索一组需要连接的所有值,然后以巧妙的方式在该子查询中使用FOR XML PATH命令。您需要连接的值来自何处并不重要,您只需要使用子查询检索它们。

我忘了提到…我还需要使用开始日期和结束日期进行筛选。Google'sql SELECT LEFT JOIN'您在哪里使用实体框架?不,我没有使用实体框架。请尝试此链接,谢谢Arno。如果有十万条记录,功能会变慢吗。。。最后,我还会加入另外4个表格。请原谅我迟来的回复。我不得不放弃。该函数无疑会带来性能成本,因为从表中构建逗号分隔的字符串会带来成本。