Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用于在.net core中获取日期内数据的Linq查询_C#_Entity Framework_Linq_Asp.net Core - Fatal编程技术网

C# 用于在.net core中获取日期内数据的Linq查询

C# 用于在.net core中获取日期内数据的Linq查询,c#,entity-framework,linq,asp.net-core,C#,Entity Framework,Linq,Asp.net Core,我想使用linq查询(.net core)选择给定日期内的数据 该实体是 公共列表日期{get;set;} 及 情况就是这样。我想选择dates日期内的所有数据,但我需要将日期与SlotDate匹配 我试过了 IEnumerable<Programs> programs = await _programRepository.GetAllAsync(p => p.Dates.Select(d => d.SlotDate >= startDate &&

我想使用linq查询(.net core)选择给定日期内的数据

该实体是

公共列表日期{get;set;}

情况就是这样。我想选择
dates
日期内的所有数据,但我需要将日期与
SlotDate
匹配

我试过了

IEnumerable<Programs> programs = await _programRepository.GetAllAsync(p => p.Dates.Select(d => d.SlotDate >= startDate && d.SlotDate<=endDate));

IEnumerable programs=await\u programRepository.GetAllAsync(p=>p.Dates.Select(d=>d.SlotDate>=startDate&&d.SlotDateLinq可以很好地处理日期时间,您可以使用标准的大于/小于运算符。非常基本的示例:

using System;
using System.Collections.Generic;
using System.Linq;
        
public class Slots
{
   public string SlotName { get; set; }
   public DateTime SlotDate { get; set; }
}

public class Program
{
    public static void Main()
    {
        List<Slots> Dates = new List<Slots>();
        Dates.Add(new Slots()
        {
            SlotName = "Test1",
            SlotDate = DateTime.Now
        });
        Dates.Add(new Slots()
        {
            SlotName = "Test2",
            SlotDate = DateTime.Now.AddSeconds(5)
        });
                      
        var selection = Dates.Where(s => s.SlotDate > DateTime.Now).Single();
        
        Console.WriteLine(selection.SlotName);
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
公务舱舱位
{
公共字符串SlotName{get;set;}
public DateTime SlotDate{get;set;}
}
公共课程
{
公共静态void Main()
{
列表日期=新列表();
日期。添加(新插槽()
{
SlotName=“Test1”,
SlotDate=DateTime.Now
});
日期。添加(新插槽()
{
SlotName=“Test2”,
SlotDate=DateTime.Now.AddSeconds(5)
});
var selection=Dates.Where(s=>s.SlotDate>DateTime.Now).Single();
Console.WriteLine(selection.SlotName);
}
}
。显然,您不会使用DateTime。现在,只需简单的“Where”行即可。如果要查找两个日期之间的值,只需添加一个
&&
和另一个条件


这只是从选择中获取一个
.Single()
值。如果要查找多个值,请使用
.ToList()
-返回的值将是一个列表,您可以像处理任何其他列表一样处理。

我尝试过了
发生了什么事?@mjwills严重性代码描述项目文件行抑制状态错误CS0029无法隐式地将类型“System.Collections.Generic.list”转换为“bool”,您可以显示给我们
GetAllAsync
?@mjwills您可以重新设置吗把它放在何处。我们最后可以添加ToListSync。程序和插槽之间的关系是什么?你能发布模型吗?可以,但我也想使用存储库。
IEnumerable programs=Wait\u programRepository.GetAllAsync(p=>p.Dates.Select(d=>d.SlotDate>=startDate&&d.SlotDate
using System;
using System.Collections.Generic;
using System.Linq;
        
public class Slots
{
   public string SlotName { get; set; }
   public DateTime SlotDate { get; set; }
}

public class Program
{
    public static void Main()
    {
        List<Slots> Dates = new List<Slots>();
        Dates.Add(new Slots()
        {
            SlotName = "Test1",
            SlotDate = DateTime.Now
        });
        Dates.Add(new Slots()
        {
            SlotName = "Test2",
            SlotDate = DateTime.Now.AddSeconds(5)
        });
                      
        var selection = Dates.Where(s => s.SlotDate > DateTime.Now).Single();
        
        Console.WriteLine(selection.SlotName);
    }
}