我怎样才能获得会员资格?

本文关键字: | 更新日期: 2023-09-27 18:08:04

您好,我有一个名为membertomship的表,其中包含列…

memberToMship_Id
memberToMship_StartDate 
memberToMship_EndDate
memberToMship_JoinFee
memberToMship_ChargePerPeriod
memberToMship_InductionFee
mshipOption_Id

还有一个名为mshipoptions的表,列为

   mshipOption_Id
   mshipOption_Period
   mshipType_Id 

和另一个表mshiptypes

  mshipType_Id
  mshipType_Name

和我的数据上下文名称是tsgdbcontext

如何将下面的查询转换为linq

"SELECT mshipType_Name, COUNT('A') AS mshipssold,
                                sum(memberToMship_InductionFee+memberToMship_JoinFee+
                                  (IF(mshipOption_Period='year',
                                  TIMESTAMPDIFF (YEAR ,memberToMship_StartDate, memberToMship_EndDate),
                                  TIMESTAMPDIFF (MONTH ,memberToMship_StartDate, memberToMship_EndDate)) * memberToMship_ChargePerPeriod)) as value
                              FROM membertomships
                              inner join mshipoptions on membertomships.mshipOption_Id = mshipoptions.mshipOption_Id
                              inner join mshiptypes on mshipoptions.mshipType_Id = mshiptypes.mshipType_Id
                              WHERE memberToMship_StartDate BETWEEN '2010-09-08' AND '2011-09-06'
                              GROUP BY mshipType_Name

我试过这样做:

修改代码:

  DateTime dateFrom = new DateTime(2010, 9, 8); 
 DateTime dateTo = new DateTime(2001, 9, 6); 
 var query = from m in tsgdbcontext.membertomship
              where m.memberToMship_StartDate  >= dateFrom && m.memberToMship_StartDate <=    dateTo 
               group m by m.mshipType_Name

我不知道下一步该做什么

我怎样才能获得会员资格?

让我们假设您已经使用关联设置了类,例如(使用CodeFirst EF)。如果使用设计器,则使用已定义的关联和类。

public class MemberToMembership
{
    [Key] // maybe also DatabaseGenerated.Identity?
    public virtual int Id { get; set; }
    public virtual DateTime StartDate { get; set; }
    public virtual DateTime StartDate { get; set; }
    public virtual decimal JoinFee { get; set; }
    public virtual decimal ChargePerPeriod { get; set; }
    public virtual decimal InductionFee { get; set; }
    public virtual int OptionId { get; set; }
    [ForeignKey("OptionId")]
    public virtual MembershipOption Option { get; set; }
}
public class MembershipOption
{
    [Key]
    public virtual int Id { get; set; }
    public virtual string Period { get; set; }
    public virtual int TypeId { get; set; }
    [ForeignKey("TypeId")]
    public virtual MembershipType Type { get; set; }
    public virtual ICollection<MemberToMembership> MemberMap { get; set; }
}
public class MembershipType
{
     [Key]
     public virtual int Id { get; set; }
     public virtual string Name { get; set; }
     public virtual ICollection<MembershipOption> Options { get; set; }
}

现在我们可以利用这些关系来帮助形成查询。

var dateFrom = new DateTime(2010, 9, 8); // start of day we care about
var dateTo = new DateTime(2011, 9, 6).AddDays(1); // end of day we care about
var query = tgsdbcontext.MemberToMemberships
                        .Where( mm => mm.StartDate > dateFrom && mm.StartDate < dateTo )
                        .GroupBy( mm => mm.Option.Type.Name )
                        .Select( g => new
                         {
                             Period = g.Key,
                             Count = g.Count(),
                             Value = g.Sum( e => e.JoinFee
                                                   + e.InductionFee
                                                   + (e.Option.Period == "year"
                                                        ? EntityFunctions.DiffYears(e.StartDate,e.EndDate) * e.ChargePerPeriod
                                                        : EntityFunctions.DiffMonths(e.StartDate,e.EndDate) * e.ChargePerPeriod))
                          });

试试这样:

DateTime dateFrom = new DateTime(2010, 9, 8); 
DateTime dateTo = new DateTime(2001, 9, 6);
var query = from t1 in tsgdbcontext.membertomship
    join t2 in tsgdbcontext.mshipoptions on t1.mshipOption_Id equals t2.mshipOption_Id
    join t3 in tsgdbcontext.mshiptypes on t1.mshipType_Id equals t3.mshipType_Id
    where t1.memberToMship_StartDate  >= dateFrom && 
        t1.memberToMship_StartDate <= dateTo 
    group t1 by t1.mshipType_Name into g
    select new { 
        mshipType_Name = g.Key, 
        mshipssold = g.Count(), 
        value = (from x in g select memberToMship_InductionFee +        
            memberToMship_JoinFee + ((mshipOption_Period = 'year' ? 
                memberToMship_EndDate.Year - memberToMship_StartDate.Year : 
                ConvertTimeSpanToMonths(memberToMship_EndDate - 
                memberToMship_StartDate)) * memberToMship_ChargePerPeriod)
        ).Sum()
    }
注意:上面的代码是未经测试的,所以可能需要调整,但这应该给你一个如何做到这一点的总体思路

您需要为ConvertTimeSpanToMonths方法编写或复制一个可用的解决方案来将TimeSpan转换为月。这里有一个你可以使用的链接:日期减法示例

相关文章:
  • 没有找到相关文章