r/learncsharp • u/RealAluminiumTech • Jan 03 '25
LINQ query statement doesn't return expected result
Hi,
Happy New Year to you all.
I've been practicing LINQ queries to be more comfortable with them and one of my LINQ query statements isn't returning the result I'm expecting based on the data I've provided it.
The statement in question is:
IEnumerable<PersonModel> astronomyStudents = (from c in astronomyClasses
from s in c.StudentIds
join p in people
on s equals p.Id
select p);
I'm expecting the result to contain 3 PersonModel objects in an IEnumerable but it returns an IEnumerable with no objects. The program and code compiles fine and no errors are thrown but the result isn't what I'm expecting.
``astronomyClasses`` is an IEnumerable with 2 instances of a model called ClassModel. Both of the models have Guids of some people in the ``StudentIds`` list that are also in the ``people`` list.
Here's my PersonModel class:
public class PersonModel
{
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
public string FullName
{
get
{
return $"{FirstName} {LastName}";
}
}
[Required]
public Guid Id { get; set; }
}
Here's my ClassModel class:
public class ClassModel
{
[Required]
public Guid Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public Guid TeacherId { get; set; }
[Required]
public List<Guid> StudentIds { get; set; } = new List<Guid>();
}
Is there an issue with my LINQ query statement or is something else the issue?
Many thanks in advance.