'How to turn a SQL query into linq query
I have the following query in C# that gets me records as follows
public async Task<List<Model>> HandleAsync(Query query)
{
var user = (await _repository.GetProjectedListAsync(q => q.Where(x => x.Created <= query.Created && x.VId == query.VId).Select(Project.Log_Model))).ToList();
return user;
}
internal static partial class Project
{
public static readonly Expression<Func<Log,Model>> Log_Model =
x => x == null ? null : new Model
{
Id = x.Id,
UserId = x.UserId,
Status = x.Status,
VId = x.VId,
created = x.Created,
};
}
I want to change the HandleAsync user query to the following SQL query:
SELECT
[Id], [Status], [Created], [VId], UserId
FROM
[Log]
WHERE
(Created) IN (SELECT MAX(Created) AS date
FROM [Log]
WHERE Created <='2022/04/29 23:25:29'
AND VId = 'c96c787c-28d3-4ac9-bc09-f3ceb2ec24d7'
GROUP BY UserId)
This SQL query fetches the latest records of multiple same users. The SQL query works well. I just don't know how to change this
var user = (await _repository.GetProjectedListAsync(q => q.Where(x => x.Created <= query.Created && x.VId == query.VId).Select(Project.Log_Model))).ToList();
return user;
to reflect the SQL changes.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
