Using Contains When Nulls Exist |
![]() ![]() |
Using Contains When Nulls Exist |
Nov 4 2009, 04:01 PM
Post
#1
|
|
|
|
I have a little problem.
I can enter this in LinqPad against my database: CODE Kits.Where(k => k.Tags.Contains("hotel")) ... and it works, even if some of the records contain null values for Tags. But if I use this same line of code in my ASP.NET MVC project using Linq-To-Sql, I get an error saying "Object Reference Not set to an instance of a object". I know I'm missing something. Can someone point me in the right direction? Thanks. KWilder |
|
|
|
Nov 7 2009, 04:48 PM
Post
#2
|
|
|
Advanced Member ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 214 Joined: 15-February 08 From: Perth, Australia Member No.: 90 |
Are you sure that you're performing a L2S and not a local query when using MVC? It's also possible that the DataContext has been set up differently.
You can try using LINQPad to query the typed DataContext that you are using in your MVC project: click 'Add Connection' and change the combo to 'Custom LINQ to SQL DataContext'. Joe |
|
|
|
Nov 10 2009, 09:01 AM
Post
#3
|
|
|
|
Are you sure that you're performing a L2S and not a local query when using MVC? It's also possible that the DataContext has been set up differently. You can try using LINQPad to query the typed DataContext that you are using in your MVC project: click 'Add Connection' and change the combo to 'Custom LINQ to SQL DataContext'. Joe Joe, I'm not quite sure I understand your statement whether I'm performing a L2S or local query, but it is going against a DataContext. I tried the connection to the DataContext in LinqPad and it also works when there are nulls in the record. I created a Repository that handles DataContext calls and I've also created a few extension methods to handle commonly repeated queries. Here's an example: Inside the calling method and a switch statement: CODE case SearchCriteria.Technology: str = value.ToString(); query = _db.KitEntities.Enabled(true).Published(true) .ToKitViewModel(SearchCriteria.Technology, str); Here are the first two extension methods: CODE public static IEnumerable<KitEntity> Enabled(this IEnumerable<KitEntity> source, bool enabled) { return source.Where(k => k.Enabled == enabled); } public static IEnumerable<KitEntity> Published(this IEnumerable<KitEntity> source, bool published) { return source.Where(k => k.Publish == published); } Here's the ToKitViewModel extension method: CODE case SearchCriteria.Technology: query = from k in source where k.Technologies.Contains(value) orderby k.ProjectName select new KitViewModel { Kit = k, Categories = null, Avg = GetAverage(k.VoteTotal, k.VoteCount) }; Everything works as it should if no column values are null. Empty strings are fine. Is there some special way to handle nulls? Thanks, KWilder |
|
|
|
Nov 17 2009, 05:29 PM
Post
#4
|
|
|
Advanced Member ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 214 Joined: 15-February 08 From: Perth, Australia Member No.: 90 |
The problem is caused by your extension methods accepting and returning IEnumerable<T> - this is forcing the query to execute locally rather than on SQL Server.
Change IEnumerable<T> to IQueryable<T> - this should fix your problem. A side effect of fixing this is that the NullReferenceException will go away because L2S queries are tolerant of subqueries returning null. Joe |
|
|
|
![]() ![]() |
|
Lo-Fi Version | Time is now: 20th November 2009 - 09:57 PM |