检查项目是否存在并在匿名类型上使用 .Contains() 返回匹配的元素

发布于 2024-11-03 02:38:43 字数 657 浏览 1 评论 0原文

我有一个日期集合(可能重复),需要使用 .Contains() 或类似方法进行检查。

我创建一个像这样的匿名类型:

var bookedDates = (from b in db.Bookings
                    where b.BookingDate > DateTime.Today
                    select new
                    {
                        Date = b.BookingDate,
                        StatusID = b.StatusId
                    });

然后我有一个日期集合 (d),并且我需要测试 d 是否存在于我的匿名类型中。使用字典这会很容易,因为我可以使用 .ContainsKey()

如果匿名类型中存在日期,我需要获取与我正在测试的日期相对应的一个或多个项目。

有没有一种快速的方法可以做到这一点,我知道我可以通过循环和测试每个键来做到这一点,但寻找更快/更有效的方法。

本质上,我正在寻找一个支持重复项目的字典。

I have a collection of dates (possibly duplicates) that I need to check against using .Contains() or a similar method.

I create an anonymous type like this:

var bookedDates = (from b in db.Bookings
                    where b.BookingDate > DateTime.Today
                    select new
                    {
                        Date = b.BookingDate,
                        StatusID = b.StatusId
                    });

I then have a collection of dates (d), and I need to test whether d exists within my anonymous type. This would be easy using a dictionary since I can use the .ContainsKey().

If a date exists within the anonymous type, I need to get the one or multiple items that correspond to the date I'm testing.

Is there a quick way to do this, I know I can do it by looping and testing each and every key but looking for a faster/more efficient way.

In essence, I'm looking for a dictionary that supports duplicate items.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情魔剑神 2024-11-10 02:38:43

如果您只想检查是否已有日期,可以使用 HashSet,然后检查以下查询中的哈希集等:

HashSet<DateTime> dates = new HashSet<DateTime>();
foreach (var item in bookedDates)
    dates.Add(item.Date);

..
if (dates.Contains(someDate))
{
    //...
}

编辑:

我认为您只想根据查询中的项目进行查找:

var dateLookup = db.Bookings
                   .Where( b => b.BookingDate > DateTime.Today)
                   .ToLookup( p => p.BookingDate, 
                              p => p.StatusId);

查找允许集合每个键的项目数,所以这可能就是您正在寻找的。

然后你就可以像这样使用它:

var statusIdsForToday = dateLookup[DateTime.Now.Date].ToList();

If you just want to check whether you have the date already you can use a HashSet, then check the hashset in the following queries etc.:

HashSet<DateTime> dates = new HashSet<DateTime>();
foreach (var item in bookedDates)
    dates.Add(item.Date);

..
if (dates.Contains(someDate))
{
    //...
}

Edit:

I think you just want a lookup based on the items in your query:

var dateLookup = db.Bookings
                   .Where( b => b.BookingDate > DateTime.Today)
                   .ToLookup( p => p.BookingDate, 
                              p => p.StatusId);

A lookup allows a collection of items for each key, so that might be what you are looking for.

Then you can just use it like this:

var statusIdsForToday = dateLookup[DateTime.Now.Date].ToList();
思慕 2024-11-10 02:38:43

var uniqueNames = bookingDates.Distinct();

var distinctNames = bookedDates.Distinct();

烏雲後面有陽光 2024-11-10 02:38:43

也许我弄错了,但您可以在 bookedDates 上应用另一个 LINQ 查询,如下所示:

DateTime searchDateTime = DateTime.Now; // or any other DateTime
var statusIds = (from b
                 in bookedDates
                 where b.Date == searchDateTime // or a similar comparison
                 select b.StatusID);
var statusIdsList = statusIds.ToList();

编辑:如果搜索日期来自同一数据库,则join 将是一个选项

var bookedIds = (from b
                 in db.Bookings
                 join dates in db.SearchDates on b.BookingDate equals dates
                 select b.StatusId);

(假设要比较的日期位于 db.SearchDates 中,如有必要,添加 where days > DateTime.Now代码>或其他日期限制)

Maybe I'm getting it wrong, but you can apply another LINQ query on your bookedDates, something like this:

DateTime searchDateTime = DateTime.Now; // or any other DateTime
var statusIds = (from b
                 in bookedDates
                 where b.Date == searchDateTime // or a similar comparison
                 select b.StatusID);
var statusIdsList = statusIds.ToList();

edit: if the search dates are from the same database then a join would be an option

var bookedIds = (from b
                 in db.Bookings
                 join dates in db.SearchDates on b.BookingDate equals dates
                 select b.StatusId);

(assuming that the dates to compare are located in db.SearchDates, if necessary, add a where dates > DateTime.Now or other restrictions on the dates)

晨敛清荷 2024-11-10 02:38:43
var bookedDates = (from b in db.Bookings
        where b.BookingDate > DateTime.Today
        select new 
        {
            date = b.BookingDate,
            statusId = b.StatusId
        }).GroupBy(x => x.date).Where(x => x.Count() > 1);

这应该为您提供一个类型为 DateTime、StatusID 类型的 IGrouping 的 IEnumerable

var bookedDates = (from b in db.Bookings
        where b.BookingDate > DateTime.Today
        select new 
        {
            date = b.BookingDate,
            statusId = b.StatusId
        }).GroupBy(x => x.date).Where(x => x.Count() > 1);

This should give you an IEnumerable of type IGrouping of type DateTime, StatusID type

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文