排序 linq 查询结果,当它有 "First() and into Group"
Sort linq query result, when it has "First() and into Group"
var qryLatestInterview = from rows in dt.AsEnumerable()
group rows by new
{
PositionID = rows["msbf_acc_cd"],
CandidateID = rows["msbf_fac_tp"]
} into grp
select grp.First();
我想使用 msbf_fac_dt
对上述结果进行排序,这是一个 DateTime 列,
所以我做了以下更改
var qryLatestInterview = from rows in dt.AsEnumerable()
orderby rows["msbf_fac_dt"] ascending
group rows by new
{
PositionID = rows["msbf_acc_cd"],
CandidateID = rows["msbf_fac_tp"],
FacilityDate = rows["msbf_fac_dt"]
} into grp
select grp.First();
但这不是按上面 msbf_fac_dt
列排序的,我在这里能做什么
您可以在选择第一条记录之前对组进行排序:
var qryLatestInterview = from rows in dt.AsEnumerable()
group rows by new
{
PositionID = rows["msbf_acc_cd"],
CandidateID = rows["msbf_fac_tp"],
} into grp
select grp.OrderBy(x=> x["msbf_fac_dt"]).First();
如果我没理解错的话,你想从这个列表中获取最近的便利记录,你可以在分组后进行排序并取第一条记录。
dt.AsEnumerable()
.GroupBy(row => new
{
PositionID = rows["msbf_acc_cd"],
CandidateID = rows["msbf_fac_tp"]
})
.Select(x=>x.OrderByDescending(o=>o["msbf_fac_dt"]>).FirstOrDefault()
.ToList();
var qryLatestInterview = from rows in dt.AsEnumerable()
group rows by new
{
PositionID = rows["msbf_acc_cd"],
CandidateID = rows["msbf_fac_tp"]
} into grp
select grp.First();
我想使用 msbf_fac_dt
对上述结果进行排序,这是一个 DateTime 列,
所以我做了以下更改
var qryLatestInterview = from rows in dt.AsEnumerable()
orderby rows["msbf_fac_dt"] ascending
group rows by new
{
PositionID = rows["msbf_acc_cd"],
CandidateID = rows["msbf_fac_tp"],
FacilityDate = rows["msbf_fac_dt"]
} into grp
select grp.First();
但这不是按上面 msbf_fac_dt
列排序的,我在这里能做什么
您可以在选择第一条记录之前对组进行排序:
var qryLatestInterview = from rows in dt.AsEnumerable()
group rows by new
{
PositionID = rows["msbf_acc_cd"],
CandidateID = rows["msbf_fac_tp"],
} into grp
select grp.OrderBy(x=> x["msbf_fac_dt"]).First();
如果我没理解错的话,你想从这个列表中获取最近的便利记录,你可以在分组后进行排序并取第一条记录。
dt.AsEnumerable()
.GroupBy(row => new
{
PositionID = rows["msbf_acc_cd"],
CandidateID = rows["msbf_fac_tp"]
})
.Select(x=>x.OrderByDescending(o=>o["msbf_fac_dt"]>).FirstOrDefault()
.ToList();