LINQ.要求了解C#中的“选择和来自何处" [英] LINQ. Request for understanding multiple select and from and where in C#

查看:53
本文介绍了LINQ.要求了解C#中的“选择和来自何处"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚接触LINQ,遇到一些我必须阅读的代码的麻烦.我不是想找任何人向我解释代码,而是,我想知道:

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:

  • 首先,什么是合适的搜索词供我学习. IE.当您有多个Select语句时,该怎么称呼.我的直觉是这是一个内部联接,但是我对这种LINQ语法不熟悉.

  • 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.

其次,可以指出一些参考资料吗?我已经尝试过,但是我认为我无法提出正确的搜索字词限制了我.我要么从简单的select语句中提出,要么从内部连接语句中提出.

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.

代码:

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;

这篇关于LINQ.要求了解C#中的“选择和来自何处"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆