在另一个Linq扩展调用中使用Linq扩展调用会引发异常

时间:2014-10-29 23:49:11

标签: c# linq linq-extensions

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();

上面的代码抛出一个null异常。

当我像下面的代码一样添加ToList()时,代码运行正常。

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

我不明白为什么。你能解释一下吗?

更多细节。 TagandTaggroup和Taggroup是数据库中的表。

第一个的SelectAllQuery是

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()是TagandTaggroup表。 cid是一个已经传递的值,所以它不是null。它里面有一个int。

这一点工作正常,直到我们升级到.Net4.5.1。我不知道这与它有什么关系。非常感谢您的解释。

请求的堆栈跟踪。

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)

0 个答案:

没有答案