2013-07-16 58 views
1

我新望對LINQ和我有一些代碼,我要讀的麻煩。我不希望任何人的代碼解釋給我聽,說,我想知道:LINQ。請理解多種選擇,並從那裏與在C#中

  • 首先,什麼是我要了解這個適當的搜索詞。即當你有多個Select語句時,你稱它爲什麼。我的直覺是這是一個內連接,但我不熟悉這個LINQ語法。

  • 其次,可以點我一些參考?我試過了,但我認爲我無法提出正確的搜索條件限制了我。我或者從select語句或者內部連接語句的位置來想出簡單的。

代碼:

var matchingReading = (from myo in _database.SessionDatabaseObject.Nations ?? new List<Nation>() 
           where myo.ReportAbbreviation.ToLower() == nationReportAbbr.ToLower() 
           select (from side in _database.SessionDatabaseObject.Sexes 
             where sex.Name.ToLower() == sexReportAbbr.ToLower() 
             select (from recSite in _database.SessionDatabaseObject.RecordingSites 
               where recSite.NationId == myo.Id 
               where recSite.SexId == sex.Id 
               select 
                (from harnCh in _database.SessionDatabaseObject.Interviewers 
                where harnCh.RecordingSiteId == recSite.Id 
                select 
                 (from reading in 
                  _database.SessionDatabaseObject.Readings 
                 where reading.InterviewerId == harnCh.Id 
                 where reading. RunEventId == _entry.Id 
                 select reading))))).ToList(); 
     if (!matchingReading.Any() || 
      !matchingReading.First().Any() || 
      !matchingReading.First().First().Any() || 
      !matchingReading.First().First().First().Any() || 
      !matchingReading.First().First().First().First().Any()) 
      return ""; 

     float? height = matchingReading.First().First().First().First().First().Height; 
     return height.HasValue ? ((int)Math.Floor(height.Value)).ToString() : ""; 
+1

這些是嵌套查詢。你可以在這裏找到的信息:http://www.sql-tutorial.com/sql-nested-queries-sql-tutorial/ –

+1

這應該是一個良好的開端爲您http://code.msdn.microsoft.com/101 -LINQ樣本-3fb9811b –

+3

該查詢似乎是有人不知道LINQ的加入來完成。 – fcuesta

回答

1

這個查詢是非常醜陋的,所以我爆炸給你。希望這對你來說更容易解析:

// Find the nations whose name matches nationReportAbbr 
// Find the nations whose name matches nationReportAbbr 
var matchingNations = _database.SessionDatabaseObject.Nations.Where(nation => 
    String.Equals(nation.ReportAbbreviation, nationReportAbbr, StringComparison.CurrentCultureIgnoreCase)); 
if (matchingNations.Any()) 
{ 
    Nation matchingNation = matchingNations.First(); 

    // Find the sexes whose name matches sexReportAbbr 
    var matchingSexes = _database.SessionDatabaseObject.Sexes.Where(sex => 
     String.Equals(sex.Name, sexReportAbbr, StringComparison.CurrentCultureIgnoreCase)); 
    if (matchingSexes.Any()) 
    { 
     Sex matchingSex = matchingSexes.First(); 

     // Find the recording sites with the appropriate nation and sex 
     var matchingRecordingSites = _database.SessionDatabaseObject.RecordingSites.Where(recordingSite => 
      recordingSite.NationId == matchingNation.Id && recordingSite.SexId == matchingSex.Id); 
     if (matchingRecordingSites.Any()) 
     { 
      RecordingSite matchingRecordingSite = matchingRecordingSites.First(); 

      // Find the interviewers with the appropriate recording site 
      var matchingInterviewers = _database.SessionDatabaseObject.Interviewers.Where(interviewer => 
       interviewer.RecordingSiteId == matchingRecordingSite.Id); 
      if (matchingInterviewers.Any()) 
      { 
       Interviewer matchingInterviewer = matchingInterviewers.First(); 

       // Find the readings taken by the appropriate interviewer whose RunEventId matches the provided _entry.Id 
       var matchingReadings = _database.SessionDatabaseObject.Readings.Where(reading => 
        reading.InterviewerId == matchingInterviewer.Id 
        && reading.RunEventId == _entry.Id); 
       if (matchingReadings.Any()) 
       { 
        Reading matchingReading = matchingReadings.First(); 

        // Find the height 
        float? height = matchingReading.Height; 
        if (height.HasValue) 
        { 
         return ((int)Math.Floor(height.Value)).ToString(); 
        } 
       } 
      } 
     } 
    } 
} 

return String.Empty;