简体   繁体   English

使用linq查询从联接表中选择多个列时如何返回值?

[英]how to return value when using linq query to select multiple columns from joined table?

I have two tables Table1 and Table2 as following:- 我有两个表Table1Table2如下:

Table1
| ServiceNumber | Name | Appointment | Department | DateofJoining|

and

Table2
|ServiceNumber | Age | Sex | SpouseName| NumberOfChildren |

now i've a following LINQ Query 现在我有以下LINQ查询

var result = from p in Table1
             join p in Table2 
             on p.ServiceNumber equals q.ServiceNumber
             select new {
                p.ServiceNumber,
                q.Name,
                q.SpouseName
             };

This is written inside a function returnSpouseDetails() . 这是在函数returnSpouseDetails()编写的。 My problem is what is going to be the return type of this function. 我的问题是该函数的返回类型是什么。 Since the returned result is not of the type of either tables(classes). 由于返回的结果不是两个表(类)的类型。 Should I write another class just to return this value or there is a better method? 我应该编写另一个类只是为了返回该值还是有更好的方法?

You can create a different class as you mentioned or else can return Tuple if you don't wanna create separate class. 您可以创建您提到的其他类,或者如果您不想创建单独的类,则可以返回Tuple。 https://msdn.microsoft.com/en-us/library/system.tuple(v=vs.110).aspx is the official link about Tuple. https://msdn.microsoft.com/zh-cn/library/system.tuple(v=vs.110).aspx是有关Tuple的官方链接。

No, there is no better way. 不,没有更好的方法。 The code that receives that return value of that function has no way of knowing what the properties of that object are, unless they're defined in a class somewhere. 接收该函数返回值的代码无法知道该对象的属性,除非它们是在某个地方的类中定义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM