简体   繁体   中英

How to write Dynamic LINQ queries using DBContext in C#

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.

您可以使用

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

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();

在此处输入图片说明

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>")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM