Enumerable.Concat not working

前端 未结 2 1628
甜味超标
甜味超标 2020-12-20 14:05

Below is the code:

string[] values = Acode.Split(\',\');
IEnumerable tst = null;

foreach (string a in values)
{
    if (tst== null)
        tst          


        
相关标签:
2条回答
  • 2020-12-20 14:40

    Concat doesn't modify anything - it returns a new sequence, which you're currently ignoring.

    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();
    

    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();
    
    0 讨论(0)
  • 2020-12-20 14:47

    That is because Concat will return a new instance of your enumerable.

    Either use in your else :

    tst = tst.Concat(...)

    Or Change your Enumerable into list from the beginning :

    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;
    
    0 讨论(0)
提交回复
热议问题