Search code examples
asp.netentity-frameworklinq-to-entities

Entity frame work calling to function changing id to name


The DriverId which will be received from this code will be a number.

protected void Page_Load(object sender, EventArgs e)
{
    OrderDataRepository rep = new OrderDataRepository();

    var results = rep.GetAllOrderData().
                  GroupBy(o => o.DRIVER_ID).
                  Select(g =>
                            new
                            {
                                DriverId = g.Key,
                                OrderCount = g.Count(),
                                OrderCountWhereNameIsNotNull = 
                                                  g.Count(o => o.RECEIVE_NAME != null)
                            }).ToList();

    DataViewer.DataSource = results;
    DataViewer.DataBind();
}

I need to change this DriverId number to a name - so I need to call another function called get and to receive a name.

public User Get(long userId)
{
    return GetQuery().Single(x => x.Id == userId);
}

public user get calling an entity "user" which has "id" which is the driverid and "name". I need the name.

enter image description here


Solution

  • Would this work?

    var results = rep.GetAllOrderData().
                  GroupBy(o => o.DRIVER_ID).
                  Select(g =>
                            new
                            {
                                DriverId = g.Key,
                                Name = g.Select(d => d.DRIVER_NAME).FirstOrDefault(),
                                OrderCount = g.Count(),
                                OrderCountWhereNameIsNotNull = 
                                                  g.Count(o => o.RECEIVE_NAME != null)
                            }).ToList();