我正在修改我的项目以使用DBContext而不是ObjectContext。 我现有的代码
$yourArray = json_decode($yourJSON, true);
$yourFormattedArray = array();
$map = array(
'true' => true,
'false' => false
// more as needed
);
foreach ($yourArray as $key => $val ) {
if ( isset($map[$val]) ) {
$val = $map[$val];
}
$yourFormattedArray[str_replace('_', '.', $key)] = $val;
}
$yourFinalArray = json_encode($yourFormattedArray);
上面的代码是使用ObjectContext编写的。 将我的项目更改为继承自DBContext后,我收到以下错误
var query = context.Vehicles.OrderBy("it.VehicleType.VehicleTypeID").
GroupBy("it.VehicleType.VehicleTypeID", "Min(it.ODO_Reading) AS MinRunVehicle, it.VehicleType.VehicleTypeID");
我想知道如何在DBContext中指定动态Linq查询。有人可以帮忙。
答案 0 :(得分:0)
您可以使用
var query = context.Vehicles.OrderBy(m=>m.it.VehicleType.VehicleTypeID)
答案 1 :(得分:0)
以下是如何转换所有内容,包括OrderBy
和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();
答案 2 :(得分:0)
第一个示例使用eSQL,而不是某种类型的动态LINQ。
DbContext不允许您直接执行eSQL查询,但您可以访问基础ObjectContext并像以前一样使用它:
var query = ((IObjectContextAdapter)context).ObjectContext
.CreateQuery<Vehicle>("<ESQL Query>")