1

我对 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() : "";
4

1 回答 1

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;
于 2013-07-24T17:11:21.577 回答