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. Ie 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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.