Question

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. enter image description here

Please help.

Was it helpful?

Solution

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.

from Anonymous Types (C# Programming Guide)

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top