How to write Dynamic LINQ queries using DBContext in C#

1.1k views Asked by At

I am modifying my project to use DBContext instead of ObjectContext. My existing code

var query = context.Vehicles.OrderBy("it.VehicleType.VehicleTypeID").
            GroupBy("it.VehicleType.VehicleTypeID", "Min(it.ODO_Reading) AS MinRunVehicle, it.VehicleType.VehicleTypeID");

The above code is written using ObjectContext. After changing my project to inherit from DBContext I am getting the below error

    Error   89  The type arguments for method 'System.Linq.Queryable.OrderBy<TSource,TKey>(System.Linq.IQueryable<TSource>, System.Linq.Expressions.Expression<System.Func<TSource,TKey>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

I want to know how to specify dynamic Linq query in DBContext. Can somebody help.

3

There are 3 answers

2
Panagiotis Kanavos On BEST ANSWER

The first example uses eSQL, not some type of dynamic LINQ.

DbContext doesn't allow you to perform eSQL queries directly, but you can get access to the underlying ObjectContext and use it as before:

var query = ((IObjectContextAdapter)context).ObjectContext
                                            .CreateQuery<Vehicle>("<ESQL Query>")
0
Jhonie On

You can use

var query = context.Vehicles.OrderBy(m=>m.it.VehicleType.VehicleTypeID)
1
Aydin On

Here's how to convert it all including OrderBy and GroupBy

void Main()
{

    var vehicles = new List<Vehicle>();

    for (int i = 0; i < 10; i++)
    {
        vehicles.Add(new Vehicle());
    }

    vehicles.OrderBy(vehicle     => vehicle.VehicleType.VehicleTypeID)
            .GroupBy(vehicleType => vehicleType.VehicleType.VehicleTypeID, 
                     vehicle     => vehicle.ODO_Reading, 
                                    (vehicleTypeID, minRunVehicle) => new 
                                    {
                                        VehicleTypeId = vehicleTypeID, 
                                        minRunVehicle = minRunVehicle
                                    })
            .ToList()
            .ForEach(vehicle => 
                     {
                         Console.WriteLine(vehicle.VehicleTypeId);
                         vehicle.minRunVehicle.ToList()
                                              .ForEach(minRun =>
                                              {
                                                  Console.WriteLine ("\t > :" + minRun);
                                              });
                         Console.WriteLine ("\n");
                     });
}

public class Vehicle
{
    public Vehicle()
    {
        this.VehicleType = new VehicleType();
        this.ODO_Reading = random.Next(100, 100000);
    }

    public VehicleType VehicleType { get; set;  }
    public int ODO_Reading { get; set; }
}

public class VehicleType
{
    public VehicleType()
    {
        VehicleTypeID = random.Next(1, 10);
    }

    public int VehicleTypeID { get; set; }
} 

public static Random random = new Random();

enter image description here