C# “中的LINQ到实体查询”;点符号;

C# “中的LINQ到实体查询”;点符号;,c#,linq,linq-to-entities,entity-framework-4.1,C#,Linq,Linq To Entities,Entity Framework 4.1,我的ASP.NETMVC3应用程序中有两个实体。我正在使用EF4.1 [Table("tblAccount")] public class Account { [Key] [Column("Creditor Registry ID", Order = 0)] public int CreditRegistryId { get; set; } [Key] [Required] [Column("Account No", Order =

我的ASP.NETMVC3应用程序中有两个实体。我正在使用EF4.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; }
}
我需要使用扩展方法“点”表示法将以下查询转换为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 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=73175269037116688L;
var excludeTypes=new[]{'CA00','CA01','CA03','CA04','CA02','PA00','PA01','PA02','PA03','PA04'};
var maxStateChangeMonth=4;
var excludeStatusId=999;
每月付款的风险价值=
上下文。帐户
.Join(context.accountowner,
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 Math.Abs(x.Account.minimumInstallation));
这里没有明确使用连接:

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 ownerRegId=73175269037116688L;
var excludeTypes=new[]{'CA00','CA01','CA03','CA04','CA02','PA00','PA01','PA02','PA03','PA04'};
var maxStateChangeMonth=4;
var excludeStatusId=999;
每月付款的风险价值=
context.accountowner
.Where(ao=>ao.AccountOwnerRegistryID==ownerRegId
&&!excludeTypes.Contains(ao.Account.AccountType)
&&(ao.Account.StateChangeDate==null | | ao.Account.StateChangeDate.Month-DateTime.Now.Month Math.Abs(ao.Account.minimumInstallation));

为什么你用什么形式写它很重要?查询符号和“点符号”一样好。总之,这是我的尝试。我相信这里使用的所有东西都应该是可翻译的

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=73175269037116688L;
var excludeTypes=new[]{'CA00','CA01','CA03','CA04','CA02','PA00','PA01','PA02','PA03','PA04'};
var maxStateChangeMonth=4;
var excludeStatusId=999;
每月付款的风险价值=
上下文。帐户
.Join(context.accountowner,
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 Math.Abs(x.Account.minimumInstallation));
这里没有明确使用连接:

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 ownerRegId=73175269037116688L;
var excludeTypes=new[]{'CA00','CA01','CA03','CA04','CA02','PA00','PA01','PA02','PA03','PA04'};
var maxStateChangeMonth=4;
var excludeStatusId=999;
每月付款的风险价值=
context.accountowner
.Where(ao=>ao.AccountOwnerRegistryID==ownerRegId
&&!excludeTypes.Contains(ao.Account.AccountType)
&&(ao.Account.StateChangeDate==null | | ao.Account.StateChangeDate.Month-DateTime.Now.Month Math.Abs(ao.Account.minimumInstallation));
var ownerRegId=73175269037116688L;
var excludeTypes=new[]
{ 
“CA00”、“CA01”、“CA03”、“CA04”、“CA02”、“PA00”、“PA01”、“PA02”、“PA03”、“PA04”
};
var maxStateChangeMonth=4;
var excludeStatusId=999;
var SumofMonthyPayments=context.AccountOwners
.Where(ao=>ao.AccountOwnerRegistryId==ownerRegId
&&!excludeTypes.Contains(ao.Account.AccountType)
&&(ao.Account.StateChangeDate==null
||ao.Account.StateChangeDate.Month-DateTime.Now.Month Math.Abs(ao.Account.minimumInstallation));

var每月付款总额=
(来自context.AccountOwners中的ao)
让a=ao.账户
其中ao.AccountOwnerRegistryId==ownerRegId
&&!excludeTypes.Contains(a.AccountType)
&&(a.StateChangeDate==null
||a.StateChangeDate.Month-DateTime.Now.Month Math.Abs(ao.Account.MinimumInstallation));
var ownerRegId=73175269037116688L;
var excludeTypes=new[]
{ 
“CA00”、“CA01”、“CA03”、“CA04”、“CA02”、“PA00”、“PA01”、“PA02”、“PA03”、“PA04”
};
var maxStateChangeMonth=4;
var excludeStatusId=999;
var SumofMonthyPayments=context.AccountOwners
.Where(ao=>ao.AccountOwnerRegistryId==ownerRegId
&&!excludeTypes.Contains(ao.Account.AccountType)
&&(ao.Account.StateChangeDate==null
||ao.Account.StateChangeDate.Month-DateTime.Now.Month Math.Abs(ao.Account.minimumInstallation));

var每月付款总额=
(来自context.AccountOwners中的ao)
让a=ao.账户
其中ao.AccountOwnerRegistryId==ownerRegId
&&!excludeTypes.Contains(a.AccountType)
&&(a.StateChangeDate==null
||a.StateChangeDate.Month-DateTime.Now.Month Math.Abs(ao.Account.MinimumInstallation));

您也可以发布您遇到的错误吗?在.Any()之后缺少一个括号无论哪种方式,any都与此无关,因为他将int与bool进行比较…安装和使用LINQPad始终是一个良好的开端如果某个帐户有多个所有者,SQL的结果不是不正确吗?您是否也可以发布您得到的错误?在.any()之后缺少一个括号无论哪种方式,any都与此无关,因为他将int与bool进行比较……安装和使用LINQPad始终是一个好的开端如果超过
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));