Abhishek
Abhishek

Reputation: 65

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

I have two tables Table1 and Table2 as following:-

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

and

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

now i've a following LINQ Query

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

Upvotes: 0

Views: 621

Answers (2)

Sathyajith
Sathyajith

Reputation: 4034

You can create a different class as you mentioned or else can return Tuple if you don't wanna create separate class. https://msdn.microsoft.com/en-us/library/system.tuple(v=vs.110).aspx is the official link about Tuple.

Upvotes: 1

James Curran
James Curran

Reputation: 103605

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.

Upvotes: 0

Related Questions