在.NET 6中,您可以使用SqlFunctions.DateDiff方法来计算两个日期之间的差异。以下是将上述SQL查询转换为.NET 6的LINQ语句的示例:

using System;
using System.Data.SqlClient;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.SqlServer.Query.Internal;

public class MyDbContext : DbContext
{
    public DbSet<MyEntity> MyEntities { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("your_connection_string");
    }
}

public class MyEntity
{
    public int Id { get; set; }
    public DateTime StartDateTime { get; set; }
    public DateTime EndDateTime { get; set; }
}

public class Program
{
    public static void Main()
    {
        using (var context = new MyDbContext())
        {
            var minutesDiff = context.MyEntities
                .Select(e => SqlServerDbFunctionsExtensions.DateDiffMinute(
                    null, // NULL value is used for culture/locale-specific rules
                    e.StartDateTime,
                    e.EndDateTime))
                .FirstOrDefault();
                
            Console.WriteLine(minutesDiff);
        }
    }
}

请注意,上述示例使用了Entity Framework Core和Microsoft SQL Server数据库提供程序。您需要根据您使用的数据库提供程序进行调整。

SELECT TIMESTAMPDIFFMINUTE 2021-08-01 120000 2021-08-01 123151;转换为NET6 linq语句

原文地址: https://www.cveoy.top/t/topic/jc8J 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录