我如何才能获得会员资格

时间:2011-09-10 09:29:37

标签: c# .net linq entity-framework linq-to-entities

您好我有一张名为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

我的datacontext名称是 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

我不知道接下来要做什么

2 个答案:

答案 0 :(得分:2)

假设您使用关联设置了类,例如(使用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))
                          });

答案 1 :(得分:1)

尝试这样的事情:

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转换为月份。这里有您可以使用的链接:Date Subtraction Examples