C
C#4mo ago
Jason_Bjorn

✅ LINQ, simple query

Is there something I can do to make this query better (faster, more idiomatic, etc)
public record Course(
string Title,
string Category,
int Number,
DateOnly PublicationDate,
TimeSpan Duration)
{
public static readonly Course[] Catalog =
[
new Course(
Title: "Elements of Geometry",
Category: "MAT", Number: 101, Duration: TimeSpan.FromHours(3),
PublicationDate: new DateOnly(2009, 5, 20)),
new Course(
Title: "Squaring the Circle",
Category: "MAT", Number: 102, Duration: TimeSpan.FromHours(7),
PublicationDate: new DateOnly(2009, 4, 1)),
new Course(
Title: "Recreational Organ Transplantation",
Category: "BIO", Number: 305, Duration: TimeSpan.FromHours(4),
PublicationDate: new DateOnly(2002, 7, 19)),
new Course(
Title: "Hyperbolic Geometry",
Category: "MAT", Number: 207, Duration: TimeSpan.FromHours(5),
PublicationDate: new DateOnly(2007, 10, 5)),
new Course(
Title: "Oversimplified Data Structures for Demos",
Category: "CSE", Number: 104, Duration: TimeSpan.FromHours(2),
PublicationDate: new DateOnly(2023, 11, 8)),
new Course(
Title: "Introduction to Human Anatomy and Physiology",
Category: "BIO", Number: 201, Duration: TimeSpan.FromHours(12),
PublicationDate: new DateOnly(2001, 4, 11)),
];
}

class Program
{
public static void Main()
{

// all the courses that are 4hrs or longer grouped by their category. What is shown: Title, Course-Number, duration
var q = Course.Catalog.Where(course => course.Duration.TotalHours >= 4).GroupBy(course => course.Category, course => new { course.Title, course.Number, course.Duration.TotalHours });
q.Dump();
}
}
public record Course(
string Title,
string Category,
int Number,
DateOnly PublicationDate,
TimeSpan Duration)
{
public static readonly Course[] Catalog =
[
new Course(
Title: "Elements of Geometry",
Category: "MAT", Number: 101, Duration: TimeSpan.FromHours(3),
PublicationDate: new DateOnly(2009, 5, 20)),
new Course(
Title: "Squaring the Circle",
Category: "MAT", Number: 102, Duration: TimeSpan.FromHours(7),
PublicationDate: new DateOnly(2009, 4, 1)),
new Course(
Title: "Recreational Organ Transplantation",
Category: "BIO", Number: 305, Duration: TimeSpan.FromHours(4),
PublicationDate: new DateOnly(2002, 7, 19)),
new Course(
Title: "Hyperbolic Geometry",
Category: "MAT", Number: 207, Duration: TimeSpan.FromHours(5),
PublicationDate: new DateOnly(2007, 10, 5)),
new Course(
Title: "Oversimplified Data Structures for Demos",
Category: "CSE", Number: 104, Duration: TimeSpan.FromHours(2),
PublicationDate: new DateOnly(2023, 11, 8)),
new Course(
Title: "Introduction to Human Anatomy and Physiology",
Category: "BIO", Number: 201, Duration: TimeSpan.FromHours(12),
PublicationDate: new DateOnly(2001, 4, 11)),
];
}

class Program
{
public static void Main()
{

// all the courses that are 4hrs or longer grouped by their category. What is shown: Title, Course-Number, duration
var q = Course.Catalog.Where(course => course.Duration.TotalHours >= 4).GroupBy(course => course.Category, course => new { course.Title, course.Number, course.Duration.TotalHours });
q.Dump();
}
}
12 Replies
reflectronic
reflectronic4mo ago
for a simple in-memory collection i don't think there is a reason for you to use the element selector you are just taking out a subset of the object, you can just have the groupings contain the objects themselves
Jason_Bjorn
Jason_Bjorn4mo ago
you mean the new {} part?
reflectronic
reflectronic4mo ago
var q = Course.Catalog.Where(course => course.Duration.TotalHours >= 4).GroupBy(course => course.Category);
var q = Course.Catalog.Where(course => course.Duration.TotalHours >= 4).GroupBy(course => course.Category);
Jason_Bjorn
Jason_Bjorn4mo ago
Well, let's say I want that information I could use a tuple instead That way I have a value tuple/struct instead of a class
reflectronic
reflectronic4mo ago
you can get that information by looking at the Course objects instead they have the information right there
Jason_Bjorn
Jason_Bjorn4mo ago
Look at them after the query?
reflectronic
reflectronic4mo ago
then you don't have to make anything if q is an IGrouping<string, Course>, then you have all the Course objects from which you can get Title, Number, and TotalHours you can do IGrouping<string, (string Title, int Number, double TotalHours)> but those values are trivial to get from the Course object, selecting them out does not really improve the code
Jason_Bjorn
Jason_Bjorn4mo ago
@reflectronic like this
var q = Course.Catalog.Where(course => course.Duration.TotalHours >= 4).GroupBy(course => course.Category);
foreach (var categoryGroup in q)
{
foreach (var course in categoryGroup)
{
Console.WriteLine($"{course.Title}, {course.Number}, {course.Duration.TotalHours}");
}
}
var q = Course.Catalog.Where(course => course.Duration.TotalHours >= 4).GroupBy(course => course.Category);
foreach (var categoryGroup in q)
{
foreach (var course in categoryGroup)
{
Console.WriteLine($"{course.Title}, {course.Number}, {course.Duration.TotalHours}");
}
}
reflectronic
reflectronic4mo ago
right
Jason_Bjorn
Jason_Bjorn4mo ago
so this is better than `new {} because it doesn't create any new data structures, it just links to existing ones or what?
Salman
Salman4mo ago
right, you don't really need to create a new object for each record apparently
Jason_Bjorn
Jason_Bjorn4mo ago
thanks
Want results from more Discord servers?
Add your server