根据您发布的查询,我了解您的模型:
public class Acs_Transaction_New_class
{
public DateTime Date {get; set;}
public string acs_operations {get; set;}
public int acs_rfid_no {get; set;}
public int acs_vehicle_id {get; set;}
public DateTime acs_datetime {get; set;}
}
public class PassLinking_Class
{
public int RfidNumber { get; set; }
public int VehicleID { get; set; }
}
流利的语法:
// Data Placeholders to compile linq query
var result = Acs_Transaction_New
.Join(Acs_Transaction_New, acs => acs.Date, acsout => acsout.Date, (acs, acsout) => new { acs, acsout })
.Where(x => x.acs.acs_operations == "in" && x.acsout.acs_operations == "out")
.Join(PassLinkings, x => new { rfid = x.acs.acs_rfid_no, vehicleid = x.acs.acs_vehicle_id }, linking => new { rfid = linking.RfidNumber, vehicleid = linking.VehicleID}, (x, linking) => new { x.acs, x.acsout,linking })
.GroupBy(x => new { x.linking.RfidNumber, x.linking.VehicleID, x.acs.Date })
.Select(x => new
{
x.Key.RfidNumber,
x.Key.VehicleID,
x.Key.Date,
acs_min_datetime = x.Min(y => y.acs.acs_datetime),
acs_max_datetime = x.Max(y => y.acsout.acs_datetime)
});
要点:
-
流利的语法比查询语法要详细得多,但是提供了清晰的数据级联,因此更容易理解
-
从Sql查询,我已经翻译了,
acs_operations == "in" / "out"
作为Where条件,不需要连接,理想情况下我们不需要与“in”和“out”进行两次比较
查询语法:
var result = from acs in Acs_Transaction_New
join acsout in Acs_Transaction_New on acs.Date equals acsout.Date
where acs.acs_operations == "in" && acsout.acs_operations == "out"
join link in PassLinkings on new { rfid = acs.acs_rfid_no, vehicleid = acs.acs_vehicle_id } equals new { rfid = link.RfidNumber, vehicleid = link.VehicleID}
group new {acs,acsout } by new { link.RfidNumber, link.VehicleID, acs.Date } into group1
select new
{
group1.Key.RfidNumber,
group1.Key.VehicleID,
group1.Key.Date,
acs_min_datetime = group1.Min(y => y.acs.acs_datetime),
acs_max_datetime = group1.Max(y => y.acsout.acs_datetime)
};
两个版本的查询都在编译中,因此语法上是正确的,您只需要进行修改以适合您的用例