【问题标题】:LINQ Left Join Causes error in SubSonic.CoreLINQ 左连接导致 SubSonic.Core 中的错误
【发布时间】:2010-01-09 17:49:29
【问题描述】:

我正在使用最新版本的 SubSonic 3 和 ActiveRecord。我正在尝试在 LINQ 中进行左连接。它在 SubSonic 中某处出现错误而失败。

我有一个 Vehicle 对象和一个 VehicleImage 对象 Vehicle 可以有多个图像,但这不是必需的。所以左连接是合适的。

这就是我所拥有的

var vehicle = from v in Vehicle.All()
              join dl in DealerLocation.All() on v.DealerLocationID equals dl.ID
              join vi in VehicleImage.All() on v.ID equals vi.VehicleID into VehicleImages
              from vij in VehicleImages.DefaultIfEmpty()
              && vij.IsPrimary
              select new
              {
                  v, vij.Image
              };

这是我得到的错误

The expression of type 'System.Linq.IQueryable`1[<>f__AnonymousType1`2[<>f__AnonymousType0`2[Project.Data.Vehicle,Project.Data.DealerLocation],System.Collections.Generic.IEnumerable`1[Project.Data.VehicleImage]]]' is not a sequence

这是堆栈跟踪

在 SubSonic.Linq.Translation.QueryBinder.ConvertToSequence(表达式 expr) 在 SubSonic.Linq.Translation.QueryBinder.VisitSequence(表达式源) 在 SubSonic.Linq.Translation.QueryBinder.BindSelectMany(类型 resultType,表达式源,LambdaExpression collectionSelector,LambdaExpression resultSelector) 在 SubSonic.Linq.Translation.QueryBinder.VisitMethodCall(MethodCallExpression m) 在 SubSonic.Linq.Structure.ExpressionVisitor.Visit(表达式 exp) 在 SubSonic.Linq.Structure.DbExpressionVisitor.Visit(表达式 exp) 在 SubSonic.Linq.Translation.QueryBinder.Visit(表达式 exp) 在 SubSonic.Linq.Translation.QueryBinder.Bind(QueryMapping 映射,表达式表达式) 在 SubSonic.Linq.Structure.QueryMapping.Translate(表达式表达式) 在 SubSonic.Linq.Structure.DbQueryProvider.Translate(表达式表达式) 在 SubSonic.Linq.Structure.DbQueryProvider.GetExecutionPlan(表达式表达式) 在 SubSonic.Linq.Structure.DbQueryProvider.Execute(表达式表达式) 在 SubSonic.Linq.Structure.QueryProvider.System.Linq.IQueryProvider.Execute(表达式表达式) 在 SubSonic.Linq.Structure.Query`1.GetEnumerator() 在 System.Linq.SystemCore_EnumerableDebugView`1.get_Items()

提前感谢您的任何见解。

【问题讨论】:

    标签: subsonic subsonic3 subsonic-active-record


    【解决方案1】:

    看起来像 SubSonic 错误。您可能想在 SubSonic 的 github 问题页面上发布此内容:http://github.com/subsonic/SubSonic-3.0/issues

    【讨论】:

      【解决方案2】:

      似乎仍然没有解决此问题的方法。一个简单的解决方法(虽然很脏)是创建一个处理左连接的视图,并用默认数据填充右侧的空数据,并让 SubSonic 在该视图上进行简单的连接。

      我知道这很糟糕,但现在已经解决了。由于这个限制,我看不到放弃 SubSonic。我相信它很快就会修复。

      【讨论】:

        【解决方案3】:

        现在尝试 Fluent Query 可能是一种解决方案。 比如:

        var DB = new myDB();
        IList<LiteObject> myLiteObject = DB.Select
            .From<Table1>()
            .InnerJoin<Table2>()
            .LeftOuterJoin<Table3>()
            .Where(Table1.IdColumn).IsEqualTo(1)
            .And(Table2.IdColumn).IsEqualTo(2)
            .ExecuteTypedList<LiteObject>();
        

        其中 LiteObject 包含所有表中的字段。

        【讨论】:

        • 看起来 Fluent Query 可以完成我现在需要的工作,但我遇到了另一个问题。我没有一个好方法可以将我的数据从多个表中获取到流利查询列表的结果中。我有许多表连接到列名与其他表相同的位置。使用 LINQ,我为这些列设置了别名。流利的查询似乎不支持这一点。因此,执行类型化列表将不起作用,因为列名与我班级中的名称不匹配。我在 Fluent Query 中遗漏了什么吗?
        • 实际上,从头开始。 Fluent Query 不会对 FindColumn 中的列名进行表限定,因此您最终会在查询中获得很多相同的列。这有各种各样的问题。猜猜我要创建一个视图。
        • 我还没有尝试,但文档说(带类型列的简单选择):int records = new NorthwindDB.Select(new string[] { ProductTable.ProductIDColumn, Product.ProductNameColumn }) .From ().GetRecordCount();
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-04
        • 1970-01-01
        相关资源
        最近更新 更多