以下是代码:
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中的所有记录,它只为数组中的最后一个值提供记录。
因此,如果我的数组包含1,2,3,4,我只得到4的记录,而我需要在tst中附加1,2,3和4的所有结果。
任何帮助都将不胜感激。
发布于 2016-06-14 15:20:54
Concat不修改任何东西-它返回一个新的序列,您目前正在忽略这个序列。
但是,与其使用Concat,不如只使用SelectMany来平缓序列:
string[] values = Acode.Split(',');
return values.SelectMany(a => entities.Test.Where(t => t.TCode == Convert.ToInt16(a)))
.ToList();或者更有效地将values转换为List<short>,然后您可以执行一个查询:
List<short> values = Acode.Split(',').Select(x => short.Parse(x)).ToList();
return entities.Test.Where(t => values.Contains(t.TCode)).ToList();发布于 2016-06-14 15:18:45
这是因为Concat将返回可枚举的新实例。
要么在你的其他地方使用:
tst = tst.Concat(...)
或从一开始就将可枚举更改为列表:
string[] values = Acode.Split(',');
List<Test> tst= new List<Test>;
foreach (string a in values)
{
tst.AddRange(entities.Test.Where(g => (g.TCode == Convert.ToInt16(a))));
}
return tst;https://stackoverflow.com/questions/37815989
复制相似问题