C#linq-如何使Join()的输出更扁平?

发布于 2025-02-04 21:38:31 字数 803 浏览 1 评论 0原文

我有一个C#Linq代码,该代码连接了三个表;我的代码看起来类似于以下(我更改了对象名称,以免揭示敏感信息):

Customers
    .Join(Products, c => c.CustomerKey, p => p.CustomerKey, (c, p) => new { c, p })
    .Join(Transactions, p => p.p.AccountNumber, t => t.AccountNumber, (p, t) => new { p.c, p.p, t })

我正在使用LinqPad进行测试,当我查看生成的SQL时,它看起来正确;实际上,当我在SSMS中执行SQL时,输出看起来完全如预期的 - 一个平面结果集,其中包含三个表中的所有列。

但是,从C#代码设置的结果看起来不同:每个记录包含三列,每个记录都存储相应的对象(客户,产品,交易),如此屏幕截图(已将敏感数据涂黑):

有没有简单的方法显示结果,使每个对象的属性被合并并水平显示为列?换句话说,以使其显示与执行SQL时相同的方式?

我当然意识到,我可以通过new {....}语句在lambda表达式末尾指定匿名对象中的每一列。但是似乎应该有一种更简单的方法来做到这一点。有没有?还是我实际上需要指定每个属性?

I have a piece of C# Linq code that joins three tables; my code looks similar to the following (I have changed object names so as not to reveal sensitive information):

Customers
    .Join(Products, c => c.CustomerKey, p => p.CustomerKey, (c, p) => new { c, p })
    .Join(Transactions, p => p.p.AccountNumber, t => t.AccountNumber, (p, t) => new { p.c, p.p, t })

I'm using LinqPad for my testing, and when I look at the generated SQL, it looks correct; in fact, when I execute the SQL in SSMS, the output looks exactly as expected - a flat result set containing all the columns from the three tables.

The result set from the C# code, however, looks different: each record contains three columns, each storing the respective objects (Customer, Product, Transaction), as in this screenshot (sensitive data has been blacked out):
enter image description here

Is there a simple way to display the results such that the properties for each object are combined and displayed horizontally as columns? In other words, so that it displays the same way as when the SQL is executed?

I realize, of course, that I could specify each column in an anonymous object via the new {....} statement at the end of the lambda expression. But it seems like there should be a simpler way to do this. Is there? Or do I in fact need to specify each property?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

将军与妓 2025-02-11 21:38:31

我当然意识到,我可以通过lambda表达式末尾的新{....}语句在匿名对象中指定每个列。

是的,这正是您需要做的。

,但似乎应该有一种更简单的方法来做到这一点。有吗?

目前,没有。

问题是:c#是一种强大的语言,enumoble.join需要定义明确的返回类型。编译器可以从提供为最终参数的lambda表达式中推断返回类型,这至少可以使您不必明确定义新类。

现在,C#语言设计师可以创建一个功能,该功能在与LINQ查询语法一起使用时自动生成新类型。但是,我认为这是极不可能的,因为(a)净福利较低,并且(b)需要做出许多困难的设计决策(如何处理两个类中具有相同名称的属性,等等。 )。

通常,C#开发人员似乎更喜欢明确性(代码确实做我写的)而不是“有用的魔术”。例如,在VB的LINQ查询语法实现中,您可以省略select子句,编译器将自动使用当前迭代变量。在C#中,类似的建议被拒绝。

I realize, of course, that I could specify each column in an anonymous object via the new {....} statement at the end of the lambda expression.

Yes, that's exactly what you need to do.

But it seems like there should be a simpler way to do this. Is there?

Currently, no.

The thing is: C# is a strongly-typed language, and Enumerable.Join needs a well-defined return type. The compiler can infer the return type from the lambda expression provided as the final argument, which at least saves you from having to define the new class explicitly.

Now, the C# language designers could create a feature that auto-generates a new type when Join is used with the LINQ query syntax. However, I consider this to be highly unlikely, since (a) the net benefit is low and (b) a lot of difficult design decisions would need to be made (how to handle properties that have the same name in both classes, etc.).

In general, C# developers seem to prefer explicitness (the code does exactly what I wrote) over "helpful magic". For example, in VB's implementation of LINQ query syntax, you can omit the select clause, and the compiler will automatically use the current iteration variable. In C#, a similar proposal was rejected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文