选择所有列加入后LINQ [英] Select all columns after JOIN in LINQ

查看:101
本文介绍了选择所有列加入后LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表,表1 表2 。我想执行,比方说,一个左外连接:

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.

我的问题是如何避免手动指定的所有属性?我想有像 SELECT * FROM表3 ,其中表3 是导致关系(后加入 TABLE1 TABLE2 )。

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).

先谢谢您的线索。

推荐答案

您有,如果你想伸入扁平型手动指定每个。另一种选择是只有你的组合式同时包含对象,这些对象自然会沿着它们的属性带来的。

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
};

和你会使用它像 myObj.Object1.Property1 myObj.Object2.Property4 等。

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

这篇关于选择所有列加入后LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆