English 中文(简体)
如果存在物品,则采用匿名类型进行核对。
原标题:Check if item exists and return matched elements using .Contains() on anonymous type

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

我制造了这样的匿名:

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

然后,我收集了<条码>(d),我需要测试<条码>d<>/条码”是否存在于我的匿名类别之内。 由于我可以使用<代码>,因此很容易使用字典。

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

如果能迅速做到这一点,我知道,我可以这样做,每个关键方面进行游说和检测,但寻求更快、更有效的方式。

从本质上讲,我期望的是支持重复项目的独裁者。

最佳回答

如果你只想检查一下你是否已经使用HashSet,然后在以下询问中查到有关点:

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

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

<><>Edit>:

页: 1 你只是想根据你的询问中的项目来审视:

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

考察可以收集每个钥匙的物品,这样或许是你所期待的。

那么,你就能够像这样使用:

var statusIdsForToday = dateLookup[DateTime.Now.Date].ToList();
问题回答

var separateNames = 预订数字。

也许我会错过,但你可以在你的<条码>上适用另一个准则查询。

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: 如果查询日期来自同一数据库,则join。 选择

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

(假设比较日期载于db.SearchDates,必要时添加,注明日期和日期; 日期。) 现在或对日期的其他限制

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





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...