选择 LINQ 中 JOIN 后的所有列

发布于 2024-12-11 12:06:33 字数 858 浏览 0 评论 0原文

我有两个表,Table1Table2。我想执行左外连接:

var myOutput = from object1 in Table1
               join object2 in Table2
               on object1.Property1 equals object2.Property2 into Table3
               from output in Table3.DefaultIfEmpty()
               select new
                   {
                       object1.Property1,
                       object1.Property2,
                       //...
                       output.Property3,
                       output.Property4,
                       //...
                   };

正如您所注意到的,我想从结果表中选择两个对象的所有属性(连接时考虑的枚举包含某些类型的对象 - 这些对于两种关系都是不同的) )。当然,我可以在匿名选择中选择属性,如示例所示。

我的问题是如何避免手动指定所有属性?我想要类似 SELECT * FROM TABLE3 的东西,其中 TABLE3 是结果关系(在加入 TABLE1TABLE2 之后)代码>)。

预先感谢您的线索。

I have two tables, Table1 and Table2. I want to perform, say, a left outer join:

var myOutput = from object1 in Table1
               join object2 in Table2
               on object1.Property1 equals object2.Property2 into Table3
               from output in Table3.DefaultIfEmpty()
               select new
                   {
                       object1.Property1,
                       object1.Property2,
                       //...
                       output.Property3,
                       output.Property4,
                       //...
                   };

As you can notice, I want to select all the properties of both objects from the resulting table (the enumerables considered while joining contain the objects of certain types - these are different for both relations). Of course, I can select the properties in the anonymous select, as shown in the example.

My question is how to avoid specifying all the properties manually? I would like to have something like SELECT * FROM TABLE3, where TABLE3 is a resulting relation (after joining TABLE1 and TABLE2).

Thanks in advance for the clues.

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

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

发布评论

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

评论(1

飘逸的'云 2024-12-18 12:06:33

如果您想投影为扁平类型,则必须手动指定每个类型。您的另一个选择是让您的组合类型包含两个对象,并且这些对象自然会带来它们的属性。

select new 
{
    Object1 = object1,
    Object2 = output
};

您可以像 myObj.Object1.Property1myObj.Object2.Property4 等一样使用它。

最后一个仍需要一些手动工作的选项是定义适当的类型并有一个构造函数或构建器方法来完成将对象属性分割为扁平类型的工作。您仍然执行手动映射,但将其与查询逻辑隔离。

select new CombinedType(object1, output);
//or 
select builder.GetCombinedType(object1, output);

You have to specify each manually if you want to project into a flattened type. Your other option is to just have your combined type contain both objects, and the objects will naturally bring along their properties.

select new 
{
    Object1 = object1,
    Object2 = output
};

And you would work with it like myObj.Object1.Property1, myObj.Object2.Property4, etc.

One final option that still involves some manual work is to define an appropriate type and have a constructor or a builder method that does the work of segmenting out your object properties into a flattened type. You still perform the manual mapping, but you isolate it from your query logic.

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