我无法以动态方式选择行。
foreach (var item in _listBox.SelectedItems)
{
Treinen treinData = (Treinen)item;
Debug.WriteLine(treinData.Name);
}
这里我从列表框中获取所选项目名称,其中包含大约60个名称,具体取决于我想要获取所选项的SQL数据的选择。我认为这需要附加一个动态的.Where子句。
ObjectQuery<Fouten> fouten = eventsEntities.Foutens;
loadedData =
(from fout in fouten
where datumStart <= fout.Datum && datumEnd >= fout.Datum
.... here should where be extended in some way, for example:
&& foreach (var item in _listBox.SelectedItems)
{
Treinen treinData = (Treinen)item;
where fout.Treinen.Name == treinData.Name
}
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.ToUpper(),
FoutId = fout.FoutId,
TreinId = fout.TreinId
}).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,
TreinId = x.TreinId
})
.ToList();
显然,这并不起作用,但希望它对我想要完成的事情有意义。现在我获取所有项目的数据而不是所选项目的数据。任何人都知道如何解决?
答案 0 :(得分:1)
我认为你的“Treinen”中有一个ID字段(或属性),因为你有你的fout.TreinId更容易找到ID。
您可以在进入查询之前填写您的ID列表,然后检查您的ID是否在列表中:
List<int> treinenIds = new List<int>();
foreach (var item in _listBox.SelectedItems)
treinenIds.Add(((Treinen)item).Id);
ObjectQuery<Fouten> fouten = eventsEntities.Foutens;
loadedData =
(from fout in fouten
where datumStart <= fout.Datum && datumEnd >= fout.Datum
//where-clause
where treinenIds.Contains(fout.TreinId)
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.ToUpper(),
FoutId = fout.FoutId,
TreinId = fout.TreinId
}).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,
TreinId = x.TreinId
})
.ToList();
我不确定查询是否支持.Contains,但如果是,那么此代码应该适用于您的示例。