“点符号”中的LINQ to Entities查询

时间:2011-04-03 08:41:40

标签: c# linq linq-to-entities entity-framework-4.1

我的ASP.Net MVC3应用程序中有两个实体。我使用的是EF 4.1

[Table("tblAccount")]
public class Account
{      
    [Key]
    [Column("Creditor Registry ID", Order = 0)]
    public int CreditRegistryId { get; set; }

    [Key]
    [Required]
    [Column("Account No", Order = 1)] 
    public int AccountNo { get; set; }

    [Column("Minimum Installment")]
    public decimal MinimumInstallment { get; set; }

    [Column("Account Status Date")]
    public DateTime AccountStatusDate { get; set; }


    [Required]
    [Column("Account Type")]
    public string AccountType { get; set; }

    public virtual ICollection<AccountOwner> AccountOwners { get; set; }
}

[Table("tblAccountOwner")]
public class AccountOwner
{
    [Key]
    [ForeignKey("Account")]
    [Column("Creditor Registry ID", Order = 0)]   
    public int CreditorRegistryId { get; set; }


    [Key]
    [ForeignKey("Account")]
    [Column("Account No", Order = 1)]           
    public int AccountNo { get; set; }

    [Key]
    [Column("Account Owner Registry ID", Order = 2)] 
    public long AccountOwnerRegistryId { get; set; }

    public virtual Account Account { get; set; }
}

我需要使用扩展方法“dot”表示法将以下查询转换为LINQ to Entities查询:

SELECT Sum(ABS([Minimum Installment])) AS SumOfMonthlyPayments
    FROM tblAccount 
    INNER JOIN tblAccountOwner ON
        tblAccount.[Creditor Registry ID] = tblAccountOwner.[Creditor Registry ID] AND
        tblAccount.[Account No] = tblAccountOwner.[Account No] 
    WHERE (tblAccountOwner.[Account Owner Registry ID] = 731752693037116688) AND
          (tblAccount.[Account Type] NOT IN  ('CA00', 'CA01', 'CA03', 'CA04', 'CA02', 'PA00', 'PA01', 'PA02', 'PA03', 'PA04')) AND
          (DATEDIFF(mm, tblAccount.[State Change Date], GETDATE()) <= 4 OR tblAccount.[State Change Date] IS NULL) AND
          (tblAccount.[Account Status ID] <> 999)

我尝试了以下查询:

var minIns = context.Accounts
                    .Where(x=>x.CreditRegistryId == x.AccountOwners.Any(z=>z.AccountOwnerRegistryId)
                    .Sum(p => Math.Abs(p.MinimumInstallment));

但它不起作用。我怎么写呢?

2 个答案:

答案 0 :(得分:0)

为什么你写的是什么形式?查询符号与“点符号”一样好。无论如何,这是我的镜头。我相信这里使用的一切都应该是可翻译的。

var ownerRegId = 731752693037116688L;
var excludeTypes = new[] { 'CA00', 'CA01', 'CA03', 'CA04', 'CA02', 'PA00', 'PA01', 'PA02', 'PA03', 'PA04' };
var maxStateChangeMonth = 4;
var excludeStatusId = 999;

var SumOfMonthlyPayments =
    context.Accounts
           .Join(context.AccountOwners,
                 a => new { CreditorRegistryId = a.CreditRegistryId, a.AccountNo },
                 ao => new { ao.CreditorRegistryId, ao.AccountNo },
                 (a, ao) => new { Account = a, AccountOwner = ao })
           .Where(x => x.AccountOwner.AccountOwnerRegistryID == ownerRegId
                    && !excludeTypes.Contains(x.Account.AccountType)
                    && (x.Account.StateChangeDate == null || x.Account.StateChangeDate.Month - DateTime.Now.Month <= maxStateChangeMonth)
                    && x.Account.AccountStatusID != excludeStatusId)
           .Sum(x => Math.Abs(x.Account.MinimumInstallment));

这里没有明确使用连接:

var ownerRegId = 731752693037116688L;
var excludeTypes = new[] { 'CA00', 'CA01', 'CA03', 'CA04', 'CA02', 'PA00', 'PA01', 'PA02', 'PA03', 'PA04' };
var maxStateChangeMonth = 4;
var excludeStatusId = 999;

var SumOfMonthlyPayments =
    context.AccountOwners
           .Where(ao => ao.AccountOwnerRegistryID == ownerRegId
                     && !excludeTypes.Contains(ao.Account.AccountType)
                     && (ao.Account.StateChangeDate == null || ao.Account.StateChangeDate.Month - DateTime.Now.Month <= maxStateChangeMonth)
                     && ao.Account.AccountStatusID != excludeStatusId)
           .Sum(ao => Math.Abs(ao.Account.MinimumInstallment));

答案 1 :(得分:0)

var ownerRegId = 731752693037116688L;
var excludeTypes = new[]
{ 
    "CA00", "CA01", "CA03", "CA04", "CA02", "PA00", "PA01", "PA02", "PA03", "PA04"
};
var maxStateChangeMonth = 4;
var excludeStatusId = 999;

var SumOfMonthlyPayments = context.AccountOwners
       .Where(ao => ao.AccountOwnerRegistryId == ownerRegId
           && !excludeTypes.Contains(ao.Account.AccountType)
           && (ao.Account.StateChangeDate == null
               || ao.Account.StateChangeDate.Month - DateTime.Now.Month <= maxStateChangeMonth)
           && ao.Account.AccountStatusID != excludeStatusId)
        .Sum(ao => Math.Abs(ao.Account.MinimumInstallment));

var SumOfMonthlyPayments =
    (from ao in context.AccountOwners
     let a = ao.Account
     where ao.AccountOwnerRegistryId == ownerRegId
         && !excludeTypes.Contains(a.AccountType)
         && (a.StateChangeDate == null
             || a.StateChangeDate.Month - DateTime.Now.Month <= maxStateChangeMonth)
         && a.AccountStatusID != excludeStatusId)
     .Sum(ao => Math.Abs(ao.Account.MinimumInstallment));