How can I translate these SQL queries into EF Core 7?
I have the following classes (tables, context etc not given for brevity):
public class Product
{
public int ProductId { get; set; }
public int Score { get; set; }
public int MarkId { get; set; }
[ForeignKey("MarkId")]
public Mark Mark { get; set; } = null!;
}
public class Mark
{
public int MarkId { get; set; }
public decimal? Value { get; set; }
public int SuperMarkId { get; set; }
[ForeignKey("SuperMarkId ")]
public SuperMark SuperMark { get; set; } = null!;
}
This is the SQL query:
SELECT
AVG(m.Value), MAX(p.Score)
FROM
Product p
JOIN
Mark m ON m.MarkId = p.MarkId
What would be the equivalent EF Core query?
Now let's add another class:
public class SuperMark
{
public int SuperMarkId { get; set; }
public decimal DefaultValue { get; set; }
}
This is the new SQL query:
SELECT
AVG(ISNULL(m.Value, sm.DefaultValue)), MAX(p.Score)
FROM
Product p
JOIN
Mark m ON m.MarkId = p.MarkId
JOIN
SuperMark sm ON sm.SuperMarkId = m.SuperMarkId
What would be the equivalent EF Core query for this SQL?