开发者

help me with the linq query syntax to read elements not in a range

I'm trying to remove all reports from a list, which are not created in a certain range of calender weeks (a property of each report). S开发者_如何转开发ince I want to learn LINQ, I started with this:

List<Reporte> reports = ReadAllReports();

List<sbyte> importantWeeks = GetRelevantWeeks(); // e.g. 49,51,52,1,2,3

// Remove irrelevant reports (from irrelevant calender weeks) doesn't work
IEnumerable<Reporte> importantReports = from oneReport in reports where oneReport.Kalenderwoche ??INRANGE importantWeeks?? select oneReport;

Could you help me with the correct syntax for the linq statement?

Thanks for any help!


I suspect you want:

var importantReports = from oneReport in reports
                       where importantWeeks.Contains(oneReport.ReportWeek)
                       select oneReport;

which could be written more concisely as:

var importantReports = reports.Where(x => importantWeeks.Contains(x.ReportWeek));

Note that if importantWeeks becomes larger, you may want to consider using HashSet<int> instead of List<int>


var importantReports = reports.Where(r => importantWeeks.Contains(r.KalenderWoche)));

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜