linq.请求了解多个选择以及从C#中的位置和地点[英] LINQ. Request for understanding multiple select and from and where in C#

本文是小编为大家收集整理的关于linq.请求了解多个选择以及从C#中的位置和地点的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我对Linq新来,并且在我必须阅读的一些代码方面遇到了麻烦.我不是在寻找任何人向我解释代码,而是想知道:

  • 首先,我有什么适当的搜索词可以了解这一点. IE.当您有多个选择语句时,您称之为什么.我的直觉是这是一个内在的联接,但我对此Linq语法不熟悉.

  • 其次,可以指出一些参考吗?我已经尝试过,但是我认为我无法提出正确的搜索词限制了我.我要么想出一个简单的选择语句,要么内部加入语句.

代码:

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

推荐答案

此查询非常丑陋,所以我为您爆炸了.希望这对您来说更容易解析:

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

本文地址:https://www.itbaoku.cn/post/1556890.html

问题描述

I'm newish to LINQ and am having trouble with some code I have to read. I'm not looking for anyone to explain the code to me , rather, I'd like to know:

  • First, What are the appropriate search terms for me to learn about this. I.e. what do you call it when you have multiple Select statement. My gut feel is that this is an inner join, but I'm not familiar with this LINQ syntax.

  • Secondly, Can point me to some references? I've tried, but I think my inability to come up with the right search terms is limiting me. I either come up with simple from where select statements, or inner join statements.

Code:

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

推荐答案

This query is pretty ugly as is, so I exploded it for you. Hopefully this is easier for you to parse:

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