0

So, I have this search query giving me an error now. (It didn't used too, fyi...) anyway, it's throwing "LINQ to Entities does not recognize the method 'System.String IfNullOrWhiteSpace"...

Is there a beter way of doing this?

 var stringResults = _propertyRepository
            .GetProperties()
            .Where(
                // standard fields
                x => x.Address.IfNullOrWhiteSpace("").Contains(searchString)
                     || x.City.IfNullOrWhiteSpace("").Contains(searchString)
                     || x.WebsiteUrl.IfNullOrWhiteSpace("").Contains(searchString)
                     || x.Zip.IfNullOrWhiteSpace("").Contains(searchString)
                    // overrides possible
                     || (x.DescriptionOverride ? x.DescriptionOverrideValue.Contains(searchString) : x.Description.IfNullOrWhiteSpace("").Contains(searchString))
                     || (x.NameOverride ? x.NameOverrideValue.Contains(searchString) : x.Name.Contains(searchString))
                     || (x.SquareFootageOverride ? x.SquareFootageOverrideValue.ToString().Contains(searchString) : x.SquareFootage.ToString().Contains(searchString))
                    // tags
                     || (x.TagsOverride ? x.TagsOverrideValue.Any(f => f.TagName.Contains(searchString)) : x.Tags.Any(f => f.TagName.Contains(searchString)))
                    // bayoptions
                     || x.BayOptions.Any(g => g.BaySizeOverride ? g.BaySizeOverrideValue.ToString().Contains(searchString) : g.BaySize.ToString().Contains(searchString))
                     || x.BayOptions.Any(g => g.DescriptionOverride ? g.DescriptionOverrideValue.IfNullOrWhiteSpace("").Contains(searchString) : g.Description.IfNullOrWhiteSpace("").Contains(searchString))
                     || x.BayOptions.Any(g => g.ExcerptOverride ? g.ExcerptOverrideValue.IfNullOrWhiteSpace("").Contains(searchString) : g.Excerpt.IfNullOrWhiteSpace("").ToString().Contains(searchString))
                    // Freeform Fields
                     || x.FreeFormFields.Any(g => g.NumberValue.ToString().Contains(searchString))
                     || x.FreeFormFields.Any(g => g.StringValue != null && g.StringValue.ToString().Contains(searchString))
            ).ToList();
Beau D'Amore
  • 3,174
  • 5
  • 24
  • 56

2 Answers2

3

IfNullOrWhiteSpace is a custom extension method, so the L2E provider does not know what to do with it.

You don't need to do any null checks when doing a Contains in L2E, so you can just remove the IfNullOrWhiteSpace part.

user247702
  • 23,641
  • 15
  • 110
  • 157
  • I was using it to catch nulls, otherwise it was breaking at this line. I've since modified my code with an additional && x.City != null to account for it and removed IfNullOrWhiteSpace. thanks again. – Beau D'Amore Aug 19 '15 at 18:05
  • FYI, I didn't write any custom extension, this was available to me without customizations. – Beau D'Amore Aug 19 '15 at 18:07
0

Unless you have defined IfNullOrWhiteSpace then the function should be IsNullOrWhiteSpace.

Gee thanks for the downvotes...possible dupe of this question if the method is spelt correctly? LINQ to Entities does not recognize the method 'System.String Format(System.String, System.Object, System.Object)'

(cue more downvotes no doubt)

Community
  • 1
  • 1
  • If the method didn't exist, the code wouldn't compile at all and OP wouldn't be getting the error he has. – user247702 Aug 19 '15 at 15:23
  • Even that (`IsNullOrWhiteSpace`) shouldn't work with LINQ to entities. – Habib Aug 19 '15 at 15:24
  • @stijn That's a build error, not a runtime error isnt it? –  Aug 19 '15 at 15:27
  • It's a runtime error. Different providers support a different feature set, so it could not possibly be a compile time error. – user247702 Aug 19 '15 at 15:27
  • fyi, i did NOT define IfNullOrWhiteSpace, it was available. – Beau D'Amore Aug 19 '15 at 20:31
  • are you sure? https://www.google.co.uk/search?q=IfNullOrWhiteSpace&oq=IfNullOrWhiteSpace&aqs=chrome..69i57j69i60l3&sourceid=chrome&es_sm=93&ie=UTF-8 –  Aug 19 '15 at 21:30