0

I'm having trouble updating an IList within an IList with a new row of data. Both Add() and AddRange() are not working out.

Please excuse my clumsy attempt to describe the landscape I'm working with.

public class DetailViewModel
{
    public int id { get; set; }
    public decimal qualScore { get; set; }
    public int testCount { get; set; }
    public string qualRating { get; set; }
    public IList<QCheckList> qualCheck { get; set; }

}

public class QCheckList
{
    public int printOrder { get; set; }
    public string description{ get; set; }
    public int? value { get; set; }
    public string grade { get; set; }
}

In the controller I have

vm = new DetailViewModel();
// other code here to read data from the DB into vm, then later...
List<QCheckList> Qual1 = calc.qualifyMark(score, subject); 

The method qualityMark() returns a List of type QCheckList.

I then want to write Qual1 to the qualCheck IList in the vm IList and have tried (unsuccessfully) both:

  • vm.qualCheck.AddRange(Qual1); error says IList<QCheckList> does not contain a definition for AddRange
  • vm.qualCheck.Add(Qual1); error says System.Collections.Generic.List<Objects.ViewModels.QCheckList> to Objects.ViewModels.QCheckList

Neither of those errors make sense to me but they probably do to one of you. Can you please explain (for a dummy like me) how to fix this so the results of the method are added to the qualCheck IList in vm. Thanks

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Dave F
  • 151
  • 14
  • Thank you both for your responses. I ended up using ```return new QCheckList (){ printOrder = 1, description = "textHere", value = 5, grade = "gradeHere"};``` The comment about naming standards is valid - I'm a very inexperienced codes, so will read through that info to try to improve. – Dave F May 07 '22 at 04:45

2 Answers2

0

IList doesn't contain a definition for AddRange. And you cannot use Add and pass a List<T> to it.

So you can fix this by something like

foreach(var item in Qual1)
    vm.qualCheck.Add(item);

You could also simply write an extension method:

static class ListExtensions
{
    static public void Add<T>(this IList<T> me, params T[] items)
    {
        foreach (T item in items)
        {
            me.Add(item);
        }
    }
}

And that should allow your code to work using simply Add. LINQ uses extension methods heavily under the covers to do what it does.

Zer0
  • 7,191
  • 1
  • 20
  • 34
  • Note: It is good idea to optimize for `List` as shown in [duplicate](https://stackoverflow.com/questions/13158121/how-to-add-a-range-of-items-to-an-ilist) I picked. – Alexei Levenkov Apr 29 '22 at 00:08
  • @AlexeiLevenkov Agree 100%. Use better performance when possible. I'd edit but no reason to with the dupe mark afaik. – Zer0 Apr 29 '22 at 01:14
0
vm = new DetailViewModel();
List<QCheckList> Qual1 = calc.qualifyMark(score, subject);

foreach(var checkList in Qual1){
    vm.qualCheck.Add(checkList);
}

Also please review the C# naming conventions here https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/coding-conventions

Properties for example qualCheck should be Capitalized

local variables for example Qual1 should not.

avalerio
  • 2,072
  • 1
  • 12
  • 11