“点表示法”中的 LINQ to Entities 查询
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
或者
or
为什么你写的形式很重要?查询表示法与“点表示法”一样好。无论如何,这是我的尝试。我相信这里使用的所有内容都应该是可翻译的。
这里没有显式使用连接:
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.
And here it is without explicitly using the join: