Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

c# - Enumerable.Concat not working

Below is the code:

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

I am not able to get all the records in tst, it is giving me records only for the last value in array.

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.

Any help will be appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...