Enumerable.Concat无法正常工作 [英] Enumerable.Concat not working
问题描述
下面是代码:
string[] values = Acode.Split(',');
IEnumerable<Test> tst = null;
foreach (string a in values)
{
if (tst== null)
tst = entities.Test.Where(t=> (t.TCode == Convert.ToInt16(a)));
else
tst.Concat(entities.Test.Where(g => (g.TCode == Convert.ToInt16(a))));
}
return tst.ToList();
我无法在tst中获取所有记录,它只为数组中的最后一个值提供记录.
I am not able to get all the records in tst, it is giving me records only for the last value in array.
因此,如果我的数组包含1,2,3,4,则仅获取4的记录.而我需要将1,2,3和4的所有结果附加到tst中.
So if my array contains 1,2,3,4 I am getting records only for the 4. Whereas i need all the result for 1,2,3 and 4 get appended in tst.
任何帮助将不胜感激.
推荐答案
Concat
不做任何修改-它返回一个新的序列,您当前正在忽略它.
Concat
doesn't modify anything - it returns a new sequence, which you're currently ignoring.
但是,您应该只使用SelectMany
来展平序列,而不是使用Concat
:
However, rather than using Concat
, you should just use SelectMany
to flatten the sequence:
string[] values = Acode.Split(',');
return values.SelectMany(a => entities.Test.Where(t => t.TCode == Convert.ToInt16(a)))
.ToList();
或更有效的方法是将values
转换为List<short>
,然后可以执行一个查询:
Or more efficiently, convert values
into a List<short>
and then you can do one query:
List<short> values = Acode.Split(',').Select(x => short.Parse(x)).ToList();
return entities.Test.Where(t => values.Contains(t.TCode)).ToList();
这篇关于Enumerable.Concat无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!