LINQ EF查询问题

时间:2014-02-25 11:30:08

标签: c# linq entity-framework

我在代码中处理linq查询的实体框架存在问题。 虽然SQL Server 2008 R2中的下面的查询,给我一组正确的记录, 我想知道这是关于我的查询或EF的某些问题,如果是EF问题,有可能解决这个问题。 每个表都有一对多的关系。如果我需要提供有关我的问题的其他信息,请告诉我。

值得一提的是,源表有足够的FrequencyID和CurrencyID可以为Nullable,在SQL Server中,当我用Null排除源表时,我得到总共360条记录,这是我期待的结果。以某种方式EF干扰这里,任何帮助将是最受欢迎的......

SQL查询

select 
    spa.PartnerNumber,
    sp.FirstName,
    sp.LastName,
    al.AllowanceName,
    spa.StartDate,
    spa.EndDate,
    fr.FrequencyDescription,
    cr.CurrencyCode,
    spa.Note 
from SAP2.PartnerAllowance spa
join MasterData..AllowanceMaster al on al.AllowanceMasterId = spa.AllowanceID
join SAP2.Partner sp on spa.PartnerNumber = sp.PartnerNumber
join MasterData.Currency cr on spa.CurrencyID = cr.CurrencyId
join MasterData..Frequency fr on spa.FrequencyID = fr.FrequencyID

相关LINQ

var sap2Partners = sap2Partner.Get().ToList();
var currency = sap2Currency.Get().ToList();
var frequency = sap2Frequency.Get().ToList();
var sap2PartnerAllowance = sap2PartnerAllowanceRepository.Get().ToList();
var allowance = sap2Allowance.Get().ToList();
var result = (from spa in sap2PartnerAllowance
              join spt in sap2Partners on spa.PartnerNumber equals spt.PartnerNumber
              join al in allowance on spa.AllowanceID equals al.AllowanceMasterId
              join cr in currency on spa.Currency.CurrencyId equals cr.CurrencyId
              join fr in frequency on spa.Frequency.FrequencyID equals fr.FrequencyID
              select new DataEntityUI
              {
                  PartnerNumber = spa.PartnerNumber,
                  FirstName = spt.FirstName,
                  LastName = spt.LastName,
                  AllowanceName = al.AllowanceName,
                  StartDate = String.Format(CurrentUserPreferences.DateFormat, spa.StartDate),
                  EndDate = String.Format(CurrentUserPreferences.DateFormat, spa.EndDate),
                  Amount = spa.Amount,
                  Frequency = fr.FrequencyDescription,
                  Currency = cr.CurrencyDesciption,
                  Note = spa.Note

              }).ToList();

1 个答案:

答案 0 :(得分:0)

可能导致问题的行是:

String.Format(CurrentUserPreferences.DateFormat, spa.StartDate)

因为您要在查询本身内格式化db值。您可以做的是,从查询中获取结果,然后将数据本地格式化为:

  var result = (from spa in sap2PartnerAllowance
                      join spt in sap2Partners on spa.PartnerNumber equals spt.PartnerNumber
                      join al in allowance on spa.AllowanceID equals al.AllowanceMasterId
                      join cr in currency on spa.Currency.CurrencyId equals cr.CurrencyId
                      join fr in frequency on spa.Frequency.FrequencyID equals fr.FrequencyID
                      select new 
                      {
                          PartnerNumber = spa.PartnerNumber,
                          FirstName = spt.FirstName,
                          LastName = spt.LastName,
                          AllowanceName = al.AllowanceName,
                          StartDate =  spa.StartDate,
                          EndDate = spa.EndDate,
                          Amount = spa.Amount,
                          Frequency = fr.FrequencyDescription,
                          Currency = cr.CurrencyDesciption,
                          Note = spa.Note

                      }).ToList();

        var result2 =result.Select(spa=>new DataEntityUI
                      {
                          PartnerNumber = spa.PartnerNumber,
                          FirstName = spa.FirstName,
                          LastName = spa.LastName,
                          AllowanceName = spa.AllowanceName,
                          StartDate = String.Format(CurrentUserPreferences.DateFormat, spa.StartDate),
                          EndDate = String.Format(CurrentUserPreferences.DateFormat, spa.EndDate),
                          Amount = spa.Amount,
                          Frequency = spa.FrequencyDescription,
                          Currency = spa.CurrencyDesciption,
                          Note = spa.Note
                      }).ToList();