Reputation: 43
I have 2 tables. ProductOrder
and Member
.
ProductOrder: OrderId, MemberId, DateTimeUTC.
Member : MemberId, FName, LName.
I want to retun a list which content OrderId, Fname, LName, DateTimeUTC.
public List<ProductOrder> GetOrderDetails()
{
using (var db = new StoreEntities())
{
var query = (from pd in db.ProductOrder
join od in db.Member on pd.MemberId equals od.MemberId
orderby od.MemberId
select new
{
pd.OrderId,
od.FName,
od.LName,
pd.DateTimeUTC,
}).ToList();
return query;
}
}
But here some error occur.
Please help.
Upvotes: 0
Views: 33
Reputation: 125610
Your query returns a collection of anonymous type instances. It shouldn't be returned from as method.
You cannot declare a field, a property, an event, or the return type of a method as having an anonymous type.
Instead, you have to create new class to store your results:
public class OrderInfo
{
public int OrderId { get; set; }
public string FName { get; set; }
public string LName { get; set; }
public DateTimeUTC { get; set; }
}
change method declaration to return collection of OrderInfo
:
public List<OrderInfo> GetOrderDetails()
and modify query to return a collection of these objects:
var query = (from pd in db.ProductOrder
join od in db.Member on pd.MemberId equals od.MemberId
orderby od.MemberId
select new OrderInfo
{
pd.OrderId,
od.FName,
od.LName,
pd.DateTimeUTC,
}).ToList();
Upvotes: 1