1

我使用 C# 和 EF5 CodeFirst。Sql Server 2012 和 VS2012

我在实体框架中具有以下形式的多个域。

    public class Subscribe : BaseEntity
{

    public Subscribe()
    {

        this.SubscribeTariffHistories = new List<SubscribeTariffHistory>();
        this.SubscribeTotalHistories = new List<SubscribeTotalHistory>();

    }

    [NotMapped]
    public bool FirstCngDate { get; set; }

    public decimal AfterSaleDebit { get; set; }
    public decimal AnnualDebit { get; set; } 
    public decimal AvrageFridayUsagePerMonth { get; set; } 

    public virtual ICollection<SubscribeTariffHistory> SubscribeTariffHistories { get; set; }
    public virtual ICollection<SubscribeTotalHistory> SubscribeTotalHistories { get; set; }

}

public class SubscribeTotalHistory : BaseEntity
{
    public SubscribeTotalHistory()
    {


    }
    public string FromDateTime { get; set; }
    public int SubscribeId { get; set; } 
    public int SubscribeTariefHistoryId { get; set; }
    public string ToDateTime { get; set; }
    public decimal? VeteranPercent { get; set; }
    public virtual Subscribe  Subscribe { get; set; }
}

    public class SubscribeTariffHistory : BaseEntity
{

    public SubscribeTariffHistory()
    {

        this.SubscribeTotalHistories = new List<SubscribeTotalHistory>();

    }
    public int BillingCalculateTypeId { get; set; } 
    public decimal FamilyCount { get; set; } 
    public string FromDateTime { get; set; } 
    public int SubscribeId { get; set; } 
    public string ToDateTime { get; set; }
    public virtual Subscribe  Subscribe { get; set; }

    public virtual ICollection<SubscribeTotalHistory> SubscribeTotalHistories { get; set; }
}

    public class BillingCalculateType : BaseEntity
{
    public BillingCalculateType()
    {
        this.SubscribeTariffHistories = new List<SubscribeTariffHistory>();
    }
    public string AdiTitle { get; set; } 
    public decimal Code { get; set; } 
    public string Title { get; set; } 


    public virtual ICollection<SubscribeTariffHistory> SubscribeTariffHistories { get; set; }
}

正如我在搜索方法中的我的业务中编写了以下命令。

        public IQueryable<SubscribeTotalHistory> Search(int subscribeId, string fromDate, string toDate)
    {
        var result = FindAll()
            .Include(i => i.Subscribe)
            .Include(i => i.SubscribeTariffHistory)
            .Include(i => i.SubscribeTariffHistory.BillingCalculateType)
            .Where(
                i =>
                (i.SubscribeId == subscribeId)
                &&
                (i.Status == AnnounceState.Valid)
                &&
                (
                    (i.FromDateTime.CompareTo(toDate) <= 0
                     && i.ToDateTime.CompareTo(fromDate) >= 0
                    )
                    ||
                    (
                        i.FromDateTime.CompareTo(fromDate) <= 0
                        &&
                        i.ToDateTime.CompareTo(toDate) >= 0
                    )
                )
            );
        return result;

    }

但我喜欢我需要获取下面的代码这一事实

SELECT 
FirstCngDate = ( CASE 
        When BCT.Code = 4712
        Then
            ( SELECT
                    TOP 1 STarH.FromDateTime 
                FROM billing.SubscribeTariffHistory AS STarH 
                WHERE ( STarH.SubscribeId = STH.SubscribeId ) 
                ORDER BY STarH.FromDateTime DESC 
            )
        ELSE
            null         
      End
    )
,   
*
FROM billing.SubscribeTotalHistory AS STH
LEFT JOIN billing.Subscribe AS S ON ( STH.SubscribeId = S.Id )
LEFT JOIN billing.SubscribeTariffHistory AS STarH ON ( STarH.id =     STH.SubscribeGenralHistoryId )
    LEFT JOIN billing.BillingCalculateType AS BCT ON ( STarH.BillingCalculateTypeId = BCT.Id )
WHERE 
STH.ToDateTime = '9999/99/99' AND STH.Status = 1

我需要的是我可以在 subscribe.FirstCngDate 中映射 FirstCngDate 吗?

如何编写LINQ,我的输出类似于上面的T/SQL。

但是,我需要我的输出地图SubscribeTotalHistory,我可以像这样使用它

SubscribeTotalHistory.Subscribe.FirstCngDate 

而且我不想在我的 Linq 中选择新的 {}。我想使用我的域。

4

0 回答 0