LINQ工会使用情况如何? [英] Linq union usage?

查看:203
本文介绍了LINQ工会使用情况如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SQL:

SELECT date,total_usage_T1 as TotalUsageValue,'T1' as UsageType FROM TblSayacOkumalari
UNION ALL
SELECT date,total_usage_T2 as TotalUsageValue,'T2' as UsageType FROM TblSayacOkumalari

和我尝试做将其转换为LINQ

And I try to do to convert it to linq

IEnumerable<TblSayacOkumalari> sayac_okumalari = entity.TblSayacOkumalari
.Select(x => new 
    { x.date, x.total_usage_T1 })
.Union(entity.TblSayacOkumalari.Select(x => new 
    { x.date, x.total_usage_T2 }));



但我不知道如何转换T1作为UsageType 来LINQ。 。还用我的工会是不正确太

But I dont know how to convert 'T1' as UsageType to linq. Also my union using is incorrect too.

像这样我的表中的字段:

My table fields like this:

| date | total_usage_T1 | total_usage_T2 |

| 2010 |             30 |             40 |
| 2011 |             40 |             45 |
| 2012 |             35 |             50 |



我要像这样

I want like this

| date | TotalUsageValue | UsageType     |

| 2010 |             30 |             T1 |
| 2011 |             40 |             T1 |
| 2012 |             35 |             T1 |
| 2010 |             40 |             T2 |
| 2011 |             45 |             T2 |
| 2012 |             50 |             T2 |



我很努力,但不能。请帮助。

I tried very hard, but could not. Please help.

推荐答案

修改

Def. from MSDN
Enumerable.Concat  - Concatenates two sequences.
Enumerable.Union    - Produces the set union of two sequences by using the default equality comparer.



我的帖子:的 CONCAT()VS联盟()

    IEnumerable<TblSayacOkumalari> sayac_okumalari = 
   entity.TblSayacOkumalari
     .Select(x => new
          {     
                date= x.date, 
                TotalUsageValue = x.total_usage_T1,
                UsageType     = "T1" 
           })
     .Concat(entity.TblSayacOkumalari
      .Select(x => new
          { 
                date= x.date,
                TotalUsageValue =  x.total_usage_T2, 
                UsageType     = "T2" }
   )); 

有关使用类型,你JUSE需要添加 UsageType =T2在新的匿名类型,因为我做了上面这将做任务给你

for usage type you juse need to add UsageType = "T2" in your new anonymous type as i did above this will do the task for you

应该比你去Concat的方法,而不是联盟的方法。

Than you should go for Concat method rather than Union method ..

示例

 int[] ints1 = { 1, 2, 3 }; int[] ints2 = { 3, 4, 5 };
 IEnumerable<INT> union = ints1.Union(ints2);
 Console.WriteLine("Union");
 foreach (int num in union)
 {
    Console.Write("{0} ", num);
 }
 Console.WriteLine();
 IEnumerable<INT> concat = ints1.Concat(ints2);
 Console.WriteLine("Concat");
 foreach (int num in concat)
 {
    Console.Write("{0} ", num);
 } 



输出

output

有关事实联盟的毗连

输出显示,concat()方法只是将两个枚举集合到单一,但并不仅仅执行任何操作/过程中的任何元素有两个枚举集合所有元素返回一个枚举集合。

The output shows that Concat() method just combine two enumerable collection to single one but doesn't perform any operation/ process any element just return single enumerable collection with all element of two enumerable collections.

联盟()方法,通过消除重复的,即如果存在相同的元素只返回单个元素返回枚举集合两者在其上进行联合枚举集合。

Union() method return the enumerable collection by eliminating the duplicate i.e just return single element if the same element exists in both enumerable collection on which union is performed.

重要一点要注意

Important point to Note


  • 通过这一事实,我们可以说,CONCAT()比联盟(),因为它没有做任何处理速度更快。

  • By this fact we can say that Concat() is faster than Union() because it doesn't do any processing.

但是,如果结合使用CONCAT()具有太多的重复元素,如果你想在创建集合进行进一步操作单个集合两个收款后需要更长的时间比()使用联盟创建的集合的方法,因为联盟()消除重复和更少的元件创建集合。

But if after combining two collection using Concat() having single collection with too many number of duplicate element and if you want to perform further operation on that created collection takes longer time than collection created using Union() method, because Union() eliminate duplicate and create collection with less elements.

这篇关于LINQ工会使用情况如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆