简体   繁体   English

使用Linq查找列表中具有特定日期的所有项目

[英]Find all items in a list with specific date using Linq

I'm trying to list the items that have a specific date and who are assigned to a particular person but for some reason, it doesn't seem to filter the list by date. 我正在尝试列出具有特定日期和分配给特定人员的项目,但是由于某种原因,它似乎没有按日期过滤列表。 The aim is to loop through each person within a group and find out what they have been tasked with by the date. 目的是在小组中的每个人之间巡视,并找出他们到日期为止所要承担的任务。

I have tried Contains and any instead but that doesn't really give me the results I'm after. 我已经尝试了Contains和其他方法,但这并没有真正给我我想要的结果。 The output of the 'Var i' just seems to be a list of all items across a wide range of dates. “ Var i”的输出似乎只是一系列日期范围内所有项目的列表。

foreach (String consultant in recipent)
            {
                var q = appointmentItems.Where(item => item.Name==consultant);
                ws.Cell(row, col).Value = consultant;
                col++;

                foreach (DateTime date in time)
                {
                    Console.WriteLine(date.ToShortDateString());
                    var i = q.Where(item => item.Date == date.ToShortDateString());
                    loggedTime = 0m;
                    //dailyHours = 7.5m;
                    availableHours = 7.5m;
                    String name = "";
                    String _date = date.ToShortDateString();

                    foreach (CalenderItem item in i)
                    {
                        //populate values and workout availibile hours
                    }
                }
            }

Calender item class 日历项目类别

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Outlook;

namespace ConsoleApplication1
{
    class CalendarItem
    {
        public String Date { get; set; }

        public decimal Duration { get; set; }

        public string Name { get; set; }

        public string Subject { get; set; }

        public bool AllDayEvent{ get; set; }

        public string Category { get; set; }

        public string BusyStatus { get; set; }

        public string Location { get; set; }
    }
}


The first linq 'Q' works fine in producing a list of items related to 1 person. 第一个linq'Q'在产生与1个人相关的项目列表时效果很好。 The second linq 'i' does nothing and returns the same list as 'Q'. 第二个linq'i'不执行任何操作,并返回与'Q'相同的列表。

 item.Date == date.ToShortDateString()

That doesn't look good 不好看

item.Date seems a date, date.TOShortDateString() is a string. item.Date似乎是日期,date.TOShortDateString()是一个字符串。

Different type, different format etc. 不同类型,不同格式等

Try to do 试着做

 item.Date.ToShortDateString() == date.ToShortDateString()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM