【发布时间】:2021-07-19 08:51:17
【问题描述】:
I have a table like this
+----+----------+------------+
| se | FKMember | Expdate |
+----+----------+------------+
| 0 | 1 | 2019-03-01 |
| 1 | 3 | 2019-03-07 |
| 2 | 2 | 2019-01-01 |
| 3 | 1 | 2019-04-01 |
| 4 | 3 | 2019-09-07 |
| 5 | 1 | 2019-05-01 |
+----+-----------------------+
我想返回一行,这一行包含会员的最大日期
我需要的结果应该是
+----+----------+------------+
| s. | FKMember | Expdate |
+----+----------+------------+
| 2 | 2 | 2019-01-01 |
| 4 | 3 | 2019-09-07 |
| 5 | 1 | 2019-05-01 |
+----+-----------------------+
我使用此代码,但它返回一行,此行包含最大日期 我应该怎么做才能让每个成员排成一行 ..................................................... ..................................................... ................................................
private void R2()
int? MemberIDAsInt = string.IsNullOrEmpty(MemberID) ? null : (int?)Convert.ToInt32(MemberID);
int? MainPackageCodeAsInt = string.IsNullOrEmpty(MainPackageCode) ? null : (int?)Convert.ToInt32(MainPackageCode);
int? SubPackageCodeAsInt = string.IsNullOrEmpty(SubPackageCode) ? null : (int?)Convert.ToInt32(SubPackageCode);
int? PackageTypeCodeAsInt = string.IsNullOrEmpty(PackageTypeCode) ? null : (int?)Convert.ToInt32(PackageTypeCode);
int? PackageCodeAsInt = string.IsNullOrEmpty(PackageCode) ? null : (int?)Convert.ToInt32(PackageCode);
int? CorporateCodeAsInt = string.IsNullOrEmpty(CorporateCode) ? null : (int?)Convert.ToInt32(CorporateCode);
int? SalesCodeAsInt = string.IsNullOrEmpty(SalesCode) ? null : (int?)Convert.ToInt32(SalesCode);
int? TrainerCodeAsInt = string.IsNullOrEmpty(TrainerCode) ? null : (int?)Convert.ToInt32(TrainerCode);
int? RelationshipCodeAsInt = string.IsNullOrEmpty(RelationshipCode) ? null : (int?)Convert.ToInt32(RelationshipCode);
int? NationalityCodeAsInt = string.IsNullOrEmpty(NationalityCode) ? null : (int?)Convert.ToInt32(NationalityCode);
bool? GenderAsBool = string.IsNullOrEmpty(Gender.ToString()) ? null : (bool?)Convert.ToBoolean(Gender);
DateTime? StartDatePackageFromAsDate = StartDatePackageFrom == string.Empty ? null : (DateTime?)Convert.ToDateTime(StartDatePackageFrom);
DateTime? StartDatePackageToAsDate = StartDatePackageTo == string.Empty ? null : (DateTime?)Convert.ToDateTime(StartDatePackageTo);
DateTime? EndDatePackageFromAsDate = EndDatePackageFrom == string.Empty ? null : (DateTime?)Convert.ToDateTime(EndDatePackageFrom);
DateTime? EndDatePackageToAsDate = EndDatePackageTo == string.Empty ? null : (DateTime?)Convert.ToDateTime(EndDatePackageTo);
using (var dbcontext = new database.GymEntities())
{
var Employee_Data = dbcontext
.VPackageForMembers
.Where(u => (MemberIDAsInt == null) ? true : u.FKMemberID == MemberIDAsInt)
.Where(u => (MainPackageCodeAsInt == null) ? true : u.FKMainPakage == MainPackageCodeAsInt)
.Where(u => (SubPackageCodeAsInt == null) ? true : u.FKSubPackage == SubPackageCodeAsInt)
.Where(u => (PackageTypeCodeAsInt == null) ? true : u.FKPackageType == PackageTypeCodeAsInt)
.Where(u => (PackageCodeAsInt == null) ? true : u.FKPackage == PackageCodeAsInt)
.Where(u => (CorporateCodeAsInt == null) ? true : u.FKCorporate == CorporateCodeAsInt)
.Where(u => (SalesCodeAsInt == null) ? true : u.FKSalesPerson == SalesCodeAsInt)
.Where(u => (TrainerCodeAsInt == null) ? true : u.FKTrainer == TrainerCodeAsInt)
.Where(u => (RelationshipCodeAsInt == null) ? true : u.FKRelationShip == RelationshipCodeAsInt)
.Where(u => (NationalityCodeAsInt == null) ? true : u.FKRelationShip == NationalityCodeAsInt)
.Where(u => (GenderAsBool == null) ? true : u.Gender == GenderAsBool)
.Where(u => (StartDatePackageFromAsDate == null) ? true : u.StartDate >= StartDatePackageFromAsDate && u.StartDate <= StartDatePackageToAsDate)
.Where(u => (EndDatePackageFromAsDate == null) ? true : u.ExpDate >= EndDatePackageFromAsDate && u.ExpDate <= EndDatePackageToAsDate)
.OrderByDescending (u=> u.ExpDate)
.Take(1)
.Select(u => new
{
u.FKMemberID,
u.MemberEnglishName,
u.MainPackageEnglishName,
u.SubPackageEnglishName,
u.PackageTypeEnglishName,
u.PackageEnglishName,
u.ExpDate,
u.PhoneNo1
}
)
.ToList();
datasource = new ReportDataSource("MemberExpireData", Employee_Data);
}
}
【问题讨论】:
-
看看一些关于使用 LINQ 进行分组的教程可能是个好主意。他们可能会有所帮助。
-
这能回答你的问题吗? Entity Framework Group By with Max Date and count ALSO 不要使用 Where like this。 EF 非常愚蠢,当 SQL Server 构建执行计划时,所有这些条件都会降低您的性能。全部为 NULL 时的事件。
-
我需要允许用户选择条件如何以更好的方式重构它??? @eocron
标签: c# entity-framework