简体   繁体   中英

LINQ EF query issue

I am having issues with entity framework handling linq queries in my code. Although the below query in SQL Server 2008 R2, gets me the correct set of records, I want to know if this some issue regarding my query or something with EF, and if it is a EF issue is there a possible way to work around this. Each table has one to many relationships. Please let me know if I need to give additional information regarding my question.

One point worth mentioning is that the Source Table has plenty of FrequencyID and CurrencyID which are Nullable, In SQL Server when I exclude the source table with Null's I get a total of 360 records, which is the result I am expecting. Somehow EF interferes here, any help will be most welcomed...

SQL Query

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

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

The line that can cause problem is:

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

Because you are formatting the db values inside the query itself. What you can do to overcome is, fetch the result from query and then locally format the data as:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM