“点表示法”中的 LINQ to Entities 查询

发布于 2024-10-28 19:16:24 字数 2216 浏览 2 评论 0原文

我的 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; }
}

我需要使用扩展方法“点”表示法将以下查询转换为 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));

但它不起作用。我该如何写呢?

I have two entities in my ASP.Net MVC3 application. I am using 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; }
}

and

[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; }
}

I need to convert following query to a LINQ to Entities query using extension method "dot" notation:

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)

I tried following query:

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

but it is not working. How can I write it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

︶葆Ⅱㄣ 2024-11-04 19:16:24
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));
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));

or

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));
旧人九事 2024-11-04 19:16:24

为什么你写的形式很重要?查询表示法与“点表示法”一样好。无论如何,这是我的尝试。我相信这里使用的所有内容都应该是可翻译的。

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));

Why would it matter what form your wrote it in? Query notation is just as good as the "dot notation." Anyhow, here's my shot at it. I believe everything used here should be translatable.

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));

And here it is without explicitly using the join:

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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文