简体   繁体   English

LINQ查询以选择表中的日期

[英]LINQ query to select date in table

I am trying to select a range of dates from Fouten.Datum, but it need to contain the dates from all the Rapporten and only for one specific NameTrein 我正在尝试从Fouten.Datum中选择日期范围,但它需要包含所有Rapporten中的日期,并且仅包含一个特定的NameTrein

My SQL diagram 我的SQL图

DateTime dateStart = CalenderSearch.SelectedDates.First();
DateTime dateEnd = CalenderSearch.SelectedDates.Last();

ObjectQuery<Fouten> fouten = eventsEntities.Foutens;
var query =
(from fout in fouten
 where dateStart <= fout.Datum && dateEnd >= fout.Datum
 orderby fout.Datum, fout.Time

 select new
 {
     Datum = fout.Datum,
     Time = fout.Time,
     FoutCode = fout.FoutCode,
     Omschrijving = fout.Omschrijving,
     Teller = fout.Teller,
     Module = fout.Module,
     FoutId = fout.FoutId

 }).AsEnumerable().Select(x => new Fouten
 {
     Datum = x.Datum,
     Time = x.Time,
     FoutCode = x.FoutCode,
     Omschrijving = x.Omschrijving,
     Teller = x.Teller,
     Module = x.Module,
     FoutId = x.FoutId
 }).ToList();

            foutensDataGrid.ItemsSource = query;

This returns all dates ignoring the foreign key's, so I'm assuming I need a select within a select within a select. 这将返回忽略外键的所有日期,因此我假设我需要在选择范围内的选择范围内进行选择。

For example I need: 例如,我需要:

all dates for NameTrein='1301' at [Treinen] inside all * [Rapporten] from [Fouten] [Fouten]中所有[Treinen]中[Treinen]的NameTrein ='1301'的所有日期

but how do I accomplice this? 但是我该如何帮忙呢?

You can add this condition to your existing where clause: 您可以将此条件添加到现有的where子句中:

var query = fouten
    .Where(fout => fout.dateStart <= fout.Datum && fout.dateEnd >= fout.Datum 
    && fout.Rapporten.Treinen.NameTrein == "1301")
    .OrderBy(fout => fout.Datum)
    .ThenBy(fout => fout.Time);

You could do this by joining the Rapporten table. 您可以通过加入Rapporten表来做到这一点。 Try this: 尝试这个:

from fout in fouten
join rapport in eventsEntities.Rapporten on rapport.RaportId equals fout.RaportId into fout_rapport
where dateStart <= fout.Datum && dateEnd >= fout.Datum && rapport.NameTrein='1301'
orderby fout.Datum, fout.Time

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

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