When Linq fails you and you can’t use Contains, try; try; again with using the right usings
Apr
15
Written by:
Monday, April 15, 2013 8:20 PM
Have you ever tried to get a .Contains() method in Linq but FOR THE LIFE OF YOU, just figure out why it’s NOT coming up? I can help you and even better, I have a simple fix!
Last week, I was playing with linq, and in my travels, ended up a pretty naïve class, created a new int[], initialized it with some values, then tried to use that in a comparison using Linq’s beautiful .Contains() method. BUT, instead of a Visual Studio Intellisense HEPING me out, it just stared at me DUMB FOUNDED! DOH! WHERE’s the darn Contains() method??????

I’ve had this happen to me before, a few times sadly, but I never really understood WHAT I was doing wrong. I slowly learned until I got the last Finger Flick last week!!!!!! FLICK! I’m missing the using System.Linq; statement for the extension methods (I THINK they’re extensions methods, please comment if you know what they’re indeed called).

ONCE I add that in, BINGO!!!!!!!!!!! NOW we’re cook’n with fire!!!!!!!!!!!!!!!!!

“Ok, so Peter, what WHERE you expecting VS to do for you?” GREAT question, I was actually expecting VS to give me the CTRL+Space shortcut you get with other missing using statements. Try typing out Debug (correct case has to be used). Notice the blue rectangle below the “D” below?

That’s Visual Studio TRYING to help you out by gently nudging you saying “pppsssssttttt hey buddy you’re missing something, can I help?” How does it help you here? (Clippy applied for the job but was rejected for being too eager! DOH!)

After you click the blue rectangle, or if you’re like me, CTRL+Space, you’ll get the usings dropdown, pick one and you’re back to compiling with F6! BEAUTIFUL! But!!!!!!!!!! In the extension methods above, I always forget with new classes I’m creating/using. DRAT! But now that I’ve blogged about it, I hope I won’t forget this the next time, and you neither!
Now that we know how to get back our Contains methods, it’s time to grab a coffee and get coding!
2 comment(s) so far...
Re: When Linq fails you and you can’t use Contains, try; try; again with using the right usings
Showcased on Jason Haley's Interesting Finds: jasonhaley.com/blog/post/2013/04/16/Interesting-Finds-April-16-2013.aspx Many thanks Jason!
By Peter Henry on
Tuesday, April 23, 2013 12:50 PM
|
Re: When Linq fails you and you can’t use Contains, try; try; again with using the right usings
I just realized today, if I want to do a .Include("SomeTable") I also need to add a using System.Data.Entity namespace at the top to get it to compile and run properly.
By Peter Henry on
Friday, December 20, 2013 11:07 AM
|