选择 LINQ 中 JOIN 后的所有列
我有两个表,Table1
和 Table2
。我想执行左外连接:
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
是结果关系(在加入 TABLE1
和 TABLE2
之后)代码>)。
预先感谢您的线索。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想投影为扁平类型,则必须手动指定每个类型。您的另一个选择是让您的组合类型包含两个对象,并且这些对象自然会带来它们的属性。
您可以像
myObj.Object1.Property1
、myObj.Object2.Property4
等一样使用它。最后一个仍需要一些手动工作的选项是定义适当的类型并有一个构造函数或构建器方法来完成将对象属性分割为扁平类型的工作。您仍然执行手动映射,但将其与查询逻辑隔离。
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.
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.