Tutorial :Linq Syntax issue



Question:

I'm having some issues with a bit of LINQ syntax and I believe I'm missing something very simple here.

I've got a basic class defined as:

public class ParseData  {      public int Offset { get; set; }      public int Length { get; set; }      public string AssociatedCode { get; set; }  }  

I have a collection of these class items that will be processed:

public ObservableCollection<ParseData> OffsetList { get; set; }  

I have a method that queries this collection to see if there are any entries that match a particular criteria and based on whether or not any do, will process the items involved differently.

Here's the syntax I'm using (LINQ syntax first in while loop):

private void ParseText()  {       //Prep code for while loop      while (currentSpacePosition != -1)      {          var possibleOffset = OffsetList.Where(offset => offset.Offset.Equals(currentCursorPosition)).ToList<ParseData>();          nextCursorPosition = currentSpacePosition + 1;          currentTextBlock = Text.Substring(currentCursorPosition,(currentSpacePosition - currentCursorPosition) + 1);          if (possibleOffset.Count != 0)          {              //Process one way;              AddHyperlinkButton(currentTextBlock);          }          else          {                         //Process another way.              AddTextBlock(currentTextBlock);          }          currentCursorPosition = nextCursorPosition;          currentSpacePosition = Text.IndexOf(' ', currentCursorPosition);      }      //More processing  }  

What am I missing here? The poosibleOffset variable keeps returning an empty list, though if I step through the code, there is an an item in the OffsetList that contains an offset prerty that will meet my criteria for selection, which suggest that my syntax isn't correct when trying to check values.

If you need more code or information about the process, I'll be happy to provide it.

Thanks in advance.

Cheers,

Steve


Solution:1

There's nothing wrong with your use of Linq really:

    public class ParseData      {          public int Offset { get; set; }       }        public ObservableCollection<ParseData> OffsetList { get; set; }        public Program()      {          OffsetList = new ObservableCollection<ParseData> { new ParseData { Offset = 5 } };          int offset = 5;          int found = OffsetList.Where(o => o.Offset.Equals(offset)).ToList().Count;          Console.WriteLine("Found: " + found);      }  

Output:

Found: 1  


Solution:2

I believe you're running into "access to modified closure" issue. You need to copy currentSpacePosition into a local temporary variable prior to Where Clause and then use that within the where criteria.


Solution:3

Could you please tell me what is the type of currentCursorPosition variable? For example, if it is of long type compiler will choose to use Equals(object) overload of Int32 and in this case it returns false even if the variable has mathematically equal value to the one in Offset property.


Solution:4

Ladies and gentlemen, please allow me to apologize for wasting your valuable time. I however, am a massive idiot. In the process of debugging some issues, I commented out the code that initialized the property OffsetList, which was the key element in my search criteria. Had I noticed this earlier, the LINQ query would have worked the first time out of the gate.

Thanks to all of you who tried to save me from my own stupidity. I will leave the question up for a short while so that you can all point and laugh, then I will close.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »