Reputation: 9613
Doing some refactoring of old code, the developer responsible long having left.
He bequeathed me this bit of Linq:
var orders = memberOrders
.Join(members, x => x.MemberID, y => y.MemberID, (x,y) => new { Order = x , y.MemberName })
.OrderByDescending(x => x.Order.MailingDate).ToList();
Which creates some sort of dual value list with an Order object connected to a MemberName string for that object.
I want a function to return this. However, I cannot figure out what type it is that the function needs to return.
Calling GetType()
reveals it's called List`1
, which isn't terribly helpful. It looks like a List<T>
constructed on the fly by the compiler into a custom object.
What is it, and how can I return it from a function?
Upvotes: 1
Views: 118
Reputation: 53958
It's a list of objects of an anonymous type with two properties.
The first property is called Order
and the second is called MemberName
.
I want a function to return this. However, I cannot figure out what type it is that the function needs to return.
It would be more meaningful then, if you could define a class with this two properties and then return a sequence of them. Like below:
public class ClassName
{
public TypeOfOrder Order { get; set; }
public TypeOfMemberName MemberName { get; set; }
}
Then you declare the method for getting the orders.
public IEnumerable<ClassName> GetOrders()
{
var orders = memberOrders.Join(members,
x => x.MemberID,
y => y.MemberID,
(x,y) => new ClassName
{
Order = x,
MemberName = y.MemberName
}).OrderByDescending(x => x.Order.MailingDate);
return orders;
}
Upvotes: 1
Reputation: 152644
It's returning an list of an anonymous type with two properties: Order
and MemberName
. Technically you can return a list of an anonymous type (the return type could be object
or List<dynamic>
), but you then require the caller to use reflection or dynamic
to access the properties.
It's safer to create a new type (or use an existing one) and return a list of that instead.
Upvotes: 8
Reputation: 58665
It's a list of an anonymous type created at:
new { Order = x , y.MemberName }
Upvotes: 1