-2

I'm troubleshooting an old application and having a hard time understanding why a variable isn't entering a foreach loop. My problem is that I have very limited experience dealing with Generics/Collections/LINQ. Please excuse me if this is an elementary question.

I have the following variable:

var allegationlist = allegation.Where(x => x.DaType == datype.FirstOrDefault().IntakeServReqTypeKey && x.DaSubType == subtypedetail.FirstOrDefault().ClassKey && x.DaNumber == daNumber);

when I mouse over each element in the where statement, they all have values. Example:

x.DaType == datype.FirstOrDefault().IntakeServReqTypeKey has a value of "Complaint-DSDS"

x.DaSubType == subtypedetail.FirstOrDefault().ClassKey has a value of "IHS"

x.DaNumber == daNumber has a value of "201706218360"

However, if I mouse over allegationlist, the Result View states "Enumeration yielded no results".

When I look into the base, Current is null as is System.Collections.IEnumerator.Current.

When I look into enumerator, Current is null and System.Collections.IEnumerator.Current is

System.Collections.IEnumerator.Current  '((System.Linq.Enumerable.WhereListIterator<CaseCompass.Intake.NewIntakeHelper.AllegationHelper>)allegationlist).enumerator.System.Collections.IEnumerator.Current' threw an exception of type 'System.InvalidOperationException'   object {System.InvalidOperationException}

If I drill into System.Collections.IEnumerator.Current the base states:

{"Enumeration has either not started or has already finished."} System.SystemException {System.InvalidOperationException} 

and the Stack Trace states:

    StackTrace  "   at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)\r\n   at System.Collections.Generic.List`1.Enumerator.System.Collections.IEnumerator.get_Current()"    string

There is also a list section with the following information:

[0] {CaseCompass.Intake.NewIntakeHelper.AllegationHelper}   CaseCompass.Intake.NewIntakeHelper.AllegationHelper
        AllegationId    {58323453-1ee3-4cef-ba2a-e1da6fd95c16}  System.Guid
        AllegationName  "Contract"  string
        DaNumber    "201706218360"  string
        DaSubType   "In-Home Services"  string
        DaType  "Complaint-DSDS"    string
        GridRowIndex    0   int
        Indicators  Count = 1   System.Collections.Generic.List<string>
        ParentGridRowIndex  0   int
        SubtypeId   "bf713051-359a-4091-9f0f-afa5019304e9"  string
        Typeid  "3cdc658e-947c-497c-8fb1-2bb9dc026baa"  string

This same information is also found in the source section

I've Googled the various error messages but I'm not experienced enough to relate what I'm reading with my code.

comfortablyNumb
  • 195
  • 1
  • 17
  • 2
    Apparently none of the items in the data source meet the condition you have specified. The fact that you successfully compute the values that your condition uses doesn't mean that there will be any items that match it. – Servy Mar 03 '17 at 21:01
  • 3
    daSubType "In-Home Services" does not match x.daSubType = "IHS" and since you are using && all must match. – Kevin Mar 03 '17 at 21:04
  • @Kevin - So the data from my variable is being checked against the source, is this correct? – comfortablyNumb Mar 03 '17 at 21:07
  • 1
    The Where clause is checking to see if any members of allegation (x) meet all of the checks. Since you are using logical and (&&), all of the checks must yield true for a member of allegation to be added to the result. – Kevin Mar 03 '17 at 21:16
  • @Kevin Thank you for the explanation. – comfortablyNumb Mar 03 '17 at 21:17
  • 1
    Be careful with those `FirstOrDefault` calls in your condition since they can return `null` and you'd be accessing a member of a null reference. If you know they'll never be empty, use `First` instead (or use `Single` if there's only supposed to ever be one item) . This way an appropriate exception can be raised when no first element is found. Otherwise use null propogation + coalescing to safeguard your conditions. Last thing we need is another "what's an NRE" dupe question around here :) – pinkfloydx33 Mar 03 '17 at 21:32

1 Answers1

2

You are looking at the wrong thing. When you hoover over a variable it will give you the result, but based on this:

x.DaType == datype.FirstOrDefault().IntakeServReqTypeKey has a value of "Complaint-DSDS"

You are not looking at the right thing. A boolean comparison cannot have a value of "Complaint-DSDS". It would be either true or false. Check what is the value of x.DaType vs the value of IntakeServReqTypeKey of the first item in the list of datype. Do this for each position in your WHERE clause.

Hope it makes sense

Renats Stozkovs
  • 2,549
  • 10
  • 22
  • 26