林克.请求理解C#中的多选和从何处选择

本文关键字:何处 选择 请求 林克 | 更新日期: 2023-09-27 18:23:42

我是LINQ新手,在阅读一些代码时遇到了问题。我不想找任何人向我解释代码,相反,我想知道:

  • 首先,我应该了解哪些合适的搜索词。也就是说,当您有多个Select语句时,您称它为什么。我的直觉是这是一个内部连接,但我不熟悉这个LINQ语法。

  • 第二,能给我指一些参考资料吗?我试过了,但我认为我无法找到正确的搜索词限制了我。我要么想出简单的from where 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() : "";

林克.请求理解C#中的多选和从何处选择

这个查询非常难看,所以我为您分解了它。希望这对你来说更容易解析:

// 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;