C# 3.0 in a Nutshell
C# 3.0 in a Nutshell, Third Edition A Desktop Quick Reference By Joseph Albahari, Ben Albahari
September 2007
Pages: 858


Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
Using Contains When Nulls Exist
kwilder
post Nov 4 2009, 04:01 PM
Post #1


New Member
*

Group: Members
Posts: 2
Joined: 4-November 09
Member No.: 20,868



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
Go to the top of the page
 
+Quote Post
JoeAlbahari
post 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
Go to the top of the page
 
+Quote Post
kwilder
post Nov 10 2009, 09:01 AM
Post #3


New Member
*

Group: Members
Posts: 2
Joined: 4-November 09
Member No.: 20,868



QUOTE (JoeAlbahari @ Nov 7 2009, 04:48 PM) *
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
Go to the top of the page
 
+Quote Post
JoeAlbahari
post 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
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

RSS Lo-Fi Version Time is now: 20th November 2009 - 09:57 PM