C# Linq 联合使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11426615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linq union usage?
提问by AliR?za Ad?yah?i
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
And I try to do to convert it to linq
我尝试将其转换为 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 }));
But I dont know how to convert 'T1' as UsageTypeto linq. Also my union using is incorrect too.
但我不知道如何转换'T1' as UsageType为 linq。我的工会使用也不正确。
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.
我很努力地尝试,但不能。请帮忙。
采纳答案by Pranay Rana
EDIT
编辑
Def. from MSDN
Enumerable.Concat - Concatenates two sequences.
Enumerable.Union - Produces the set union of two sequences by using the default equality comparer.
My post : Concat() vs Union()
我的帖子:Concat() vs Union()
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" }
));
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
对于使用类型,您需要UsageType = "T2"像我上面所做的那样添加新的匿名类型,这将为您完成任务
Than you should go for Concat method rather than Union method ..
比你应该去 Concat 方法而不是 Union 方法..
Example
例子
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
输出


Fact about Union and Concat
关于 Union 和 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.
输出显示 Concat() 方法只是将两个可枚举集合组合为一个,但不执行任何操作/处理任何元素,只返回单个可枚举集合与两个可枚举集合的所有元素。
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.
Union() 方法通过消除重复项来返回可枚举集合,即如果执行联合的两个可枚举集合中存在相同的元素,则只返回单个元素。
Important point to Note
重要注意事项
By this fact we can say that Concat() is faster than Union() because it doesn't do any processing.
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.
根据这个事实,我们可以说 Concat() 比 Union() 更快,因为它不做任何处理。
但是,如果在使用 Concat() 组合两个集合后,单个集合具有太多重复元素,并且如果您想对该创建的集合执行进一步操作比使用 Union() 方法创建的集合需要更长的时间,因为 Union() 消除了重复并用更少的元素创建集合。
回答by Pablo Romeo
In order to get the expected property names on the anonymous type you probably want to do something like:
为了在匿名类型上获得预期的属性名称,您可能需要执行以下操作:
new { x.date, TotalUsage = x.total_usage_T1, UsageType="T1" }
and also
并且
new { x.date, TotalUsage = x.total_usage_T2, UsageType="T2" }
回答by RePierre
Use this:
用这个:
var result = entity.TblSayacOkumalari
.Select(x => new
{
Date = x.date,
TotalUsage = x.total_usage_T1,
UsageType = "T1"
})
.Union(entity.TblSayacOkumalari.Select(x => new
{
Date = x.date,
TotalUsage = x.total_usage_T2,
UsageType = "T2"
}));

