MySQL TIMESTAMPDIFF to .NET 6 LINQ Conversion: Calculate Time Difference in Minutes
In .NET 6, you can utilize Entity Framework Core to execute LINQ queries. For your MySQL query, you can use the following LINQ statement:
using Microsoft.EntityFrameworkCore;
using System;
public class YourDbContext : DbContext
{
public DbSet<YourEntity> YourEntities { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseMySQL("your_connection_string");
}
}
public class YourEntity
{
public int Id { get; set; }
public DateTime StartDateTime { get; set; }
public DateTime EndDateTime { get; set; }
}
public class Program
{
static void Main(string[] args)
{
using (var dbContext = new YourDbContext())
{
var minutesDiff = dbContext.YourEntities
.Select(e => EF.Functions.TimestampDiff('MINUTE', e.StartDateTime, e.EndDateTime))
.FirstOrDefault();
Console.WriteLine(minutesDiff);
}
}
}
Make sure to replace 'your_connection_string' with your actual MySQL connection string and define appropriate properties in your entity class to match the table structure in your database. In this example, the entity class is 'YourEntity' and contains 'StartDateTime' and 'EndDateTime' fields to align with your query.
原文地址: https://www.cveoy.top/t/topic/giKa 著作权归作者所有。请勿转载和采集!