1
var tagByGroupIds = TagandTaggroupsservice.SelectAllQuery()
                    .Where(p => p.Tagid == i.TagId)
                    .Select(p => p.Taggroupid);

var tagGroups = Taggroupsservice.SelectAllQuery()
                .Where(p => tagByGroupIds.Contains(p.Taggroupid)).ToList();

The above code throws a null exception.

When I add ToList() like the code below then the code runs fine.

    var tagByGroupIds = TagandTaggroupsservice.SelectAllQuery()
                    .Where(p => p.Tagid == i.TagId)
                    .Select(p => p.Taggroupid).ToList();

I dont understand why. Can you please explain?.

More details. TagandTaggroup and Taggroup are tables in the database.

SelectAllQuery for the first one is

var query = from tt in SelectAll()
            join tg in TagGroups on tt.Taggroupid equals tg.Taggroupid
            where tg.cid== cid
            select tt;
return query.Distinct();

SelectAll() in this case is the TagandTaggroup table. cid is a value already passed by so it is not null. It has an int inside of it.

This was working fine until we upgraded to .Net4.5.1. I dont know if this has anything to do with it. An explanation would be very appreciated.

The Stack Trace as requested.

System.NullReferenceException was unhandled by user code
HResult=-2147467261 
Message=Object reference not set to an instance of an object.
Source=System.Data.Linq

StackTrace:
   at System.Data.Linq.SqlClient.SqlFactory.Member(SqlExpression expr, MemberInfo member)
   at System.Data.Linq.SqlClient.QueryConverter.VisitMemberAccess(MemberExpression ma)
   at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
   at System.Data.Linq.SqlClient.QueryConverter.VisitExpression(Expression exp)
   at System.Data.Linq.SqlClient.QueryConverter.VisitBinary(BinaryExpression b)
   at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
   at System.Data.Linq.SqlClient.QueryConverter.VisitExpression(Expression exp)
   at System.Data.Linq.SqlClient.QueryConverter.VisitWhere(Expression sequence, LambdaExpression predicate)
   at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
   at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
   at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
   at System.Data.Linq.SqlClient.QueryConverter.VisitSelect(Expression sequence, LambdaExpression selector)
   at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
   at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
   at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
   at System.Data.Linq.SqlClient.QueryConverter.VisitDistinct(Expression sequence)
   at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
   at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
   at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
   at System.Data.Linq.SqlClient.QueryConverter.VisitWhere(Expression sequence, LambdaExpression predicate)
   at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
   at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
   at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
   at System.Data.Linq.SqlClient.QueryConverter.VisitSelect(Expression sequence, LambdaExpression selector)
   at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
   at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
   at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
   at System.Data.Linq.SqlClient.QueryConverter.VisitContains(Expression sequence, Expression value)
   at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
   at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
   at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
   at System.Data.Linq.SqlClient.QueryConverter.VisitExpression(Expression exp)
   at System.Data.Linq.SqlClient.QueryConverter.VisitWhere(Expression sequence, LambdaExpression predicate)
   at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
   at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
   at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
   at System.Data.Linq.SqlClient.QueryConverter.ConvertOuter(Expression node)
   at System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations)
   at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
   at System.Data.Linq.DataQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at ***.***.TagByProductcontroller.getTagGroupsByProduct(Int32 productId)
Zein Sleiman
  • 254
  • 2
  • 11
  • You should post the full exception message, including the stack trace – Lucas Trzesniewski Oct 29 '14 at 23:52
  • I don't know if .net version can make a difference. However, it should not run on previous version too because tagbyGroupID is not executed and you cannot use inside another linq expression till you don't execute the same and execution happens as soon as List() is called. If you want to use nested then you need to use join just like you are using in SelectAll. – codebased Oct 30 '14 at 00:00
  • Thank you for the comments. Based on your comments and because I had a hunch that the problem is in the SelectAllQuery() I tried to run the same code using SelectAll() which doesnt do any join and it runs perfectly. Even with deferred execution the result is achieved. @codebased you are right, .net version has nothing to do with this. The old code didnt need the join and that explains why before it worked. So since the Join is the problem does that mean the Linq to SQL converter doesnt know how convert the statement? – Zein Sleiman Oct 30 '14 at 05:18

0 Answers0