Home

SQL - HighlightIf vs Highlight ?

Hello,

Would someone please point out why HighlightIf fails below?
( from x in
  // ensure a list of non-null first names
  ( from y in Users .Where (y => y.FirstName.Length>0) select y.FirstName ) .Take(9)
  select new {
    //  works fine:
    //  FirstName=Util.Highlight(x)
    //  fails: "Object reference not set to an instance of an object"
    FirstName=Util.HighlightIf(x, a => a.Contains("a"))
  }
)
Thanks in advance,
Ken

Comments

  • Apparently the provider is able to infer Highlight is a local method but trips on HighlightIf. Maybe it's the predicate. Not sure but adding AsEnumerable() fixes it:
    (from x in
      (from y in Users .Where (y => y.FirstName.Length>0) 
       select y.FirstName) 
      .Take(9)
      .AsEnumerable()
      select new 
      {
        FirstName = Util.HighlightIf(x, a => a.Contains("a"))
      }
    )
    You might consider fluent syntax:
    Users
    .Where(a => a.FirstName.Length > 0)
    .Select(a => a.FirstName)
    .Take(9)
    .AsEnumerable()
    .Select(a => Util.HighlightIf(a, b => b.Contains("a"))
  • Yes, it's most likely the predicate. AsEnumerable is a good workaround.
  • Thank you Sorax and Joe!
    I like fluent syntax, but was using (let) query variables in the larger query this snippet came from. :smile:
Sign In or Register to comment.