0

enter code hereI have GlobalTerms class that contains a data member terms which is a list of type Term.

Each Term class has 3 members.

  1. int Id;
  2. bool IsApproved;
  3. string Note;

so, it looks like:

Class GlobalTerms
{
  IEnumerable<TermInfo> UserTerms;

}

Class TermInfo
{
    public int Id { get; set; }
    public string Note{ get; set; }
    public bool IsApproved{ get; set; }
}

If I have an GlobalTerms object (globalTerms), how can I get a TermInfo object where the Status in TermInfo equals True?

I tried to write something like this:

     GlobalTerms globalTerms = new GlobalTerms();
                globalTerms.UserTerms.Where(x => x.IsApproved.Equals(true))
                    .Select(x => x.Id);

My task was checking that all items in UserTerms has true in IsApproved field; In result I received error: System.ArgumentNullException: Value cannot be null. Parameter name: source Thank you in advance for your help,

ANSWER: In result, this issue was solved used the next code:

 *termsList* - is a IList<GlobalTerms> 

foreach (var singleTerm in termsList)
            {
                var sourceTermsList = singleTerm.UserTerms.ToList();
                foreach (var status in sourceTermsList)
                {
                    status.IsApproved.Should().BeTrue();
                }
            }

Thank Guys to everyone,

Cindy
  • 568
  • 7
  • 20
  • 1
    The NRE is because `GlobalTerms iTerms = new GlobalTerms();` and then `iTerms.UserTerms.Where....`, You haven't instantiated `UsersTerms` in your object, it is `Null` and when you try to use `Where` on it, you get exception. Once that is fixed you can modify your condition to `Where(x=> x.IsApproved)` – Habib Aug 03 '16 at 13:31
  • I would hope it is that simple but the above code would never do anything useful without that list being populated from some other source, so we might just be missing relevant code. – S.C. Aug 03 '16 at 13:33
  • Habib, thanks a lot for your idea:) It's helps me to solve this issue. – Cindy Aug 03 '16 at 19:02

2 Answers2

0

you created new object and its property UserTerms is null

shady youssery
  • 430
  • 2
  • 17
0

You never initialize the UserTerms member in GlobalTerms, so when you create a new instance of GlobalTerms the UserTerms IEnumerable is null. You need to set it to a non-null collection of some sort before you can run linq queries against it.

Also, what is the IsApproved member? If that's a nullable type that might be your issue.

S.C.
  • 1,152
  • 8
  • 13
  • Sorry, i made mistake, isApproved is here: Class TermInfo { public int Id { get; set; } public string Note{ get; set; } public bool isApproved{ get; set; } } – Cindy Aug 03 '16 at 13:38