score:1

Accepted answer

The problem that I see is that you're selecting an anonymous type, can't you do:

public static List<object> GetCustomerOrdersCount()
{
        using (OrdersDbEntities context = new OrdersDbEntities())
        {
            return context.Customers.Select().ToList<Customer>();
        }
}

That would return your complete Customer entity, rather than an anonymous type that contains only specific members.

EDIT

If the underlying problem, in regards to our conversation about lazy loading, is that you want a count of the number of orders for a given customer, without actually loading the orders, then I would do something like this:

public class CustomerWithOrderCount
{
    public CustomerWithOrderCount(Customer c, int OrderCount) 
    { 
        Customer = c; 
        this.OrderCount = OrderCount;
    }
    public Customer { get; set; }
    public int OrderCount { get; set; }
}

public static List<object> GetCustomerOrdersCount()
{
        using (OrdersDbEntities context = new OrdersDbEntities())
        {
            return context.Customers.Select(
                c => new CustomerWithOrderCount(c, c.Orders.Count())
                             .ToList();
        }
}

score:2

public class MiniCustomerDto
{
  public int CustomerId{get;set;}
  public String CustomerName{get;set;}
  public int OrdersCount{get;set;}
}

public static List<MiniCustomerDto> GetCustomerOrdersCount()
{
        using (OrdersDbEntities context = new OrdersDbEntities())
        {
            return context.Customers.Select(c => new MiniCustomerDto
            {
                CustId = c.CustomerId,
                CustName = c.CustomerName,
                OrdersCount = c.Orders.Count
            }).ToList();
        }
}

you have to use class you can't create a method that return anonymous type
abd this is the best practice check,
Is there a way to return Anonymous Type from method?
Return anonymous type results?
by the way Dto stand for Data Transfer Object

score:0

 List<tbl_GameConfig> gameConfig = new List<tbl_GameConfig>();
 using (Entities con = new Entities())
  {
       gameConfig = con.tbl_GameConfig.Where(p => p.fk_GameTypeId == gameTypeId).ToList<tbl_GameConfig>();
  }       

Related Articles