尝试使用 XPO 编写下一个 MSSQL 查询时,我打破了主意:
SELECT Gate.Name, grp.maxDate as 'Latest pass', a.Type, a.ReceivedTime as 'Imported at'
FROM Access AS a INNER JOIN
(SELECT GateId, MAX(OpenTime) AS maxDate
FROM Access
GROUP BY GateId) AS grp ON a.GateId = grp.GateId AND a.OpenTime = grp.maxDate INNER JOIN
Gate ON a.GateId = Gate.ID
order by Gate.ID
我的 Access 表有大约 2 个工厂记录,但它只有 40 个不同的 GateId。我想为每个门选择一条线,如下所示:
GateName OpenTime Type Imported at
......................... ..................................................... ...
超级门 20/09/2013 1 21/09/2013
超级门 19/09/2013 0 22/09/2013
我的 Access 类如下所示:
public partial class Access : XPLiteObject
{
Gate fGateId;
[Association(@"AccessReferencesGate")]
public Gate GateId
{
get { return fGateId; }
set { SetPropertyValue<Gate>("GateId", ref fGateId, value); }
}
DateTime fOpenTime;
public DateTime OpenTime
{
get { return fOpenTime; }
set { SetPropertyValue<DateTime>("OpenTime", ref fOpenTime, value); }
}
byte fType;
public byte Type
{
get { return fType; }
set { SetPropertyValue<byte>("Type", ref fType, value); }
}
DateTime fReceivedTime;
public DateTime ReceivedTime
{
get { return fReceivedTime; }
set { SetPropertyValue<DateTime>("ReceivedTime", ref fReceivedTime, value); }
}
int fID;
[Key(true)]
public int ID
{
get { return fID; }
set { SetPropertyValue<int>("ID", ref fID, value); }
}
public Access(Session session) : base(session) { }
}
我的门类:
public partial class Gate : XPLiteObject
{
int fID;
[Key(true)]
public int ID
{
get { return fID; }
set { SetPropertyValue<int>("ID", ref fID, value); }
}
string fName;
[Size(20)]
public string Name
{
get { return fName; }
set { SetPropertyValue<string>("Name", ref fName, value); }
}
}
[Association(@"AccessReferencesGate", typeof(Access))]
public XPCollection<Access> AccessCollection { get { return GetCollection<Access>("AccessCollection"); } }
public Gate(Session session) : base(session) { }
}
任何答案,甚至是 RTFM 链接都将不胜感激!