使用LINQ统计指定日期下EventID唯一的记录数
Solution to Count Distinct EventIDs by Date
Got it, let's tweak your existing LINQ query to count unique EventIDs for the specified date instead of just all matching records. Here's the adjusted code:
@Model.DateAndTimes .Where(f => f.DateAndTimeDateType.Value.DayOfYear == renderedDayWithTime.DayOfYear) .Select(f => f.EventID) .Distinct() .Count()
How this works:
- The
Whereclause stays exactly as your original code, filtering records to only those matching the target date's day of year. Select(f => f.EventID)pulls out just the EventID values from the filtered records—we don't need the rest of the data for counting unique IDs.Distinct()removes duplicate EventID entries, so even if multiple records share the same EventID on the same date, it only keeps one instance.- Finally,
Count()gives you the total number of unique EventIDs for that date, which matches your example scenario (where 5 records with the same EventID would count as 1).
内容的提问来源于stack exchange,提问作者Mischa Morf
相关产品推荐
相关产品推荐

