LINQ查询在表中选择日期

时间:2016-03-02 11:29:33

标签: c# sql wpf linq

我正在尝试从Fouten.Datum中选择一系列日期,但它需要包含所有Rapporten的日期,并且只包含一个特定的NameTrein

My SQL diagram

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;

这会返回忽略外键的所有日期,所以我假设我需要在select中的select中选择。

例如我需要:

来自[Fien]的所有* [Rapporten]中[Treinen]的NameTrein ='1301'的所有日期

但我该怎样帮助这个?

2 个答案:

答案 0 :(得分:1)

您可以将此条件添加到现有的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);

答案 1 :(得分:0)

您可以通过加入Rapporten表来完成此操作。试试这个:

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