- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在尝试使用 Join 和 GroupJoin 方法。问题看起来很简单。给定 TableA
和 TableB
作为数据映射,这样:
class MyDataContext : DataContext
{
public Table<tblA> TableA;
public Table<tblB> TableB;
}
...我正在使用 TableA
作为我的主表,并希望加入 TableB
中的一个字段 CustomerID
以检索 [TableB].[姓氏]
。
应该不难,只是我很难让结果正常工作。 TableA 有我想要的记录,而不管 TableB 中是否有匹配的 CustomerID。听起来像左连接 - 所以,阅读 here ,我模仿了@tvanfosson 的建议:
// appropriately rewritten for my needs - so I thought...
private static IQueryable GetRecordsByView1(IQueryable<tblA> source)
{
var records = source.GroupJoin(myContext.TableB,
info => info.CustomerID,
owner => owner.CustomerID,
(info, owner) => new
{
info.CustomerID,
Owner = owner.Select(o => o.LastName).DefaultIfEmpty(),
Store = info.Store,
})
.Select(record => new
{
record.CustomerID,
record.Owner,
record.Store,
});
return records;
}
source
是动态的,因此一个方法构建了一个动态查询:
public static void QueryStores()
{
IQueryable<tblA> source = myContext.TableA;
if (criteriaA)
source = source.Where(// something);
if (criteriaB)
source = source.Where(// something);
// after processing criteria logic, determine type of view
switch (byView)
{
case View1:
{
source = GetRecordsByView1(source);
break;
}
//other case blocks
}
myGridView.DataSource = source;
}
问题:我收到以下错误:
Could not format node 'OptionalValue' for execution as SQL.
我相信它在以下代码行中:
Owner = owner.Select(o => o.LastName).DefaultIfEmpty()
我在这里做错了什么?我必须将 GroupJoin
编写为扩展方法。
最佳答案
您是正确的,Owner = owner.Select(o => o.LastName).DefaultIfEmpty()
是引起您问题的行。我想出的最好的解决方法是这样的:
var records = source.GroupJoin(myContext.TableB,
info => info.CustomerID,
owner => owner.CustomerID,
(info, owner) => new { info, owner }).ToList();
records.Select(x => new
{
x.info.CustomerID,
Owner = x.owner.First() == null ? new string[] {} : x.owner.Select(o => o.LastName).ToArray(),
Store = x.info.Store,
})
.Select(record => new
{
record.CustomerID,
record.Owner,
record.Store,
});
这当然不理想(您必须使用“ToList”来具体化 groupjoin),并且可能有更好的解决方案,但这对我有用。您可能需要尝试一下才能让它适合您,但我希望这对您有所帮助。
关于c# - 正确的 Join/GroupJoin 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4114110/
我有这 2 个列表 list1 和 list2 并创建了这个 GroupJoin: List1 是人员列表(Id、姓名),list2 是对象列表(Id、Car、Date)。 PS:list1 包含大约
在 GroupJoin 之后,我想包括 Book.Authors,因为字段 Book.Authors 将为空。但我不能。我应该怎么做才能解决这个问题? Queryable recent = Repos
我想在两个集合之间执行 GroupJoin,但基于除相等性之外的其他谓词。例如,如果我有一个包含项目的集合,每个项目都包含一个范围属性,我想将它们中的每一个与另一个集合中的项目相关联,该集合的某些属性
基本上,我想做的是左外连接两个表并将它们呈现在一个平面结果中。为简单起见,我的两个表如下所示: tot["nameA", "nameB", "nameC"] critItg["nameA", "nam
我对使用 Entity Framework 还很陌生,并且一直遇到代码无法编译的问题。我收到的错误消息是: The type arguments for method 'System.Linq.Que
我有一个看起来(简化)如下的数据库: var reports = new[] { new { ReportId = 1, Title = "Report A" }, new { Rep
我想花 5-6 个小时来了解有关 GroupJoin 的一些事情。我正在谈论使用 linq 查询数据库表。我了解 Join 和 GroupJoin 的作用,但我真的不明白我在哪里需要它。我觉得 Joi
我有以下代码 var profilesWithMailLists = profilesWithCf.GroupJoin( profile2Maillist,
我该如何解释(严重?)? 我有一个名为“规则”的实体,它是一些用于从 POCO[] 中挑选项目的“选择器”。有一定数量的字段可供选择。所有这些都在我的数据库对象中有详细说明。 我需要在网格中显示规则,
我有一个返回如下结果的 GroupJoin 查询: IEnumerable query = ICDUnitOfWork.AlphaGroups.Find() .GroupJoin(ICDUni
The GroupJoin method has no direct equivalent in relational database terms, but it implements a supe
这是我的代码显示的错误 Severity Code Description Project File Line Suppression State Error CS0411 The type argu
我一直在尝试使用 Join 和 GroupJoin 方法。问题看起来很简单。给定 TableA 和 TableB 作为数据映射,这样: class MyDataContext : DataContex
我已经生成/测试了两个要组合起来执行单个查询的 Observable。 一个用户可以有多个角色。每当他们的角色 ID 发生变化时,就需要更新数据。但是数据应该只在查询处于事件状态时更新(当前有一些控件
谁能解释一下 GroupJoin() 是什么? 它与常规 Join() 有何不同? 常用吗? 它只针对方法语法吗?查询语法呢? (一个 c# 代码示例会很好) 最佳答案 行为 假设您有两个列表: Id
我不明白为什么这不翻译。这似乎正是描述的用例 here . LINQ 表达式 DbSet() .GroupJoin( inner: DbSet(), outer
这是我第一次使用 GroupJoin。从我看到的示例来看,在其基本形式中使用似乎非常简单,但是当我使用它时,我总是会遇到 NavigationExpandingExpressionVisitor 异常
以下代码: const Rx = require('rx'); const gameRowObservable = Rx.Observable.fromArray([{ game_id: 1,
我正在尝试使用 GroupJoin 和 SelectMany 在 linq 中执行“左外连接”,但随后我还想使用 GroupBy 和 Sum 聚合结果。 但是当我执行下面的代码时,我得到: Syste
我有以下类(class) public class OrderItem { public Guid ID { get; set; } public Guid? ParentID { g
我是一名优秀的程序员,十分优秀!