0

I am trying to find out which List<int> out of many has a certain amount of elements

Example input:

List<int> list_1 = Enumerable.Range(1, 299).ToList();
List<int> list_2 = Enumerable.Range(1, 300).ToList();
List<int> list_3 = Enumerable.Range(1, 300).ToList();
List<int> list_4 = Enumerable.Range(1, 297).ToList();

What I need is to log the name of the list and the amount of items if it has less than let's say 300 values. Now I could do of course a single if clause for every list:

if (list_1.Count != 300)
{
    //log name and number of items
}
if(list_2 ... and so on)

Question: Is there a more elegant way of finding out this information?

I tried a LINQ solution and put all Lists into one for filtering. But in this my problem is that I cannot get the name:

List<List<int>> allLists = new List<List<int>>();

allLists.Add(list_1);
allLists.Add(list_2);
allLists.Add(list_3);
allLists.Add(list_4);

string logRes = String.Join(" ", allLists.Where(x=>x.Count < 300)
        .Select(x=> String.Format("Name: {0} Amount: {1}", nameof(x), x.Count)));

It returns:

Name: x Amount: 299 Name: x Amount: 297

Question 2: How can I get the name of the evil list in the collection?

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76

2 Answers2

3

Either use reflection or a different collection like f.e. aDictionary<string, List<int>>:

var listNames = new Dictionary<string, List<int>>();
listNames.Add(nameof(list_1), list_1);
listNames.Add(nameof(list_2), list_2);
listNames.Add(nameof(list_3), list_3);
listNames.Add(nameof(list_4), list_4);

string logRes = String.Join(" ", listNames
    .Where(kv => kv.Value.Count < 300)
    .Select(kv => $"Name: {kv.Key} Amount: {kv.Value.Count}"));
Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Thank you for the dictionary suggestion. Is this the best way to avoid multiple if-clauses? – Mong Zhu May 09 '17 at 06:26
  • @MongZhu: specify _best_, in my opinion its a readable and concise version, yes. – Tim Schmelter May 09 '17 at 07:47
  • trying to specify: less code but still readable. It gets pretty wild to read if I would have 10 lists for example. But then again may be I should rethink the design of saving the data in the first place maybe. I like your solution, thanx again – Mong Zhu May 09 '17 at 07:51
0

You may modify your code to include the names of lists along with actual lists:

Dictionary<string, List<int>> allLists = new Dictionary<string, List<int>>();

allLists.Add("list_1", list_1);
allLists.Add("list_2", list_2);
allLists.Add("list_3", list_3);
allLists.Add("list_4", list_4);

string logRes = String.Join(" ", allLists
    .Where(x => x.Value.Count < 300)
    .Select(x => String.Format("Name: {0} Amount: {1}", x.Key, x.Value.Count)));
sachin
  • 2,341
  • 12
  • 24
  • Your answer is exact the same as @Tim Schmelter.. But he was 4 mintues before you.. – Sean Stayns May 08 '17 at 15:08
  • @SeanStayn Well not exactly same, that one is going to work only after C# 6.0. Looks like I took quite some time in typing my answer. – sachin May 08 '17 at 15:23