我的 WPF/MVVM 表单中的详细网格包含来自分组 LINQ 查询的数据
Detailed grid in my WPF/MVVM form with data from grouped LINQ query
我正在使用 Visual Studio 2015 C#,WPF/MVVM,Devexpress,
EntityFramework6 和代码优先到 Oracle 数据库。
我的数据库(传输和日志)中有两个 table,我需要从日志 table 中按 TransportID 获取分组的日志列表,然后将传输详细信息添加到此列表,然后创建具有这两个列表的 GridControl。你能指导我吗,我做错了什么
首先我创建了实体:
public partial class Transport
{
[Key]
public int TransportID { get; set; }
public string Brand { get; set; }
public string Model { get; set; }
public class Journal
{
[Key]
public int JournalID { get; set; }
public int TransportID { get; set; }
[ForeignKey("TransportID")]
public virtual Transport Transport { get; set; }
public DateTime JournalDate { get; set; }
}
public class JournalGrouped
{
public int JournalID { get; set; }
public int TransportID { get; set; }
public string Brand { get; set; }
public string Model { get; set; }
public DateTime JournalDate { get; set; }
public List<Journal> TransportJournal { get; set; }
}
然后在 ViewModel 中构建列表:
var journal = dbContext.Journals.AsQueryable();
var groupedList = journal.GroupBy(j => new { j.TransportID}).ToList();
var filteredList = journal.Where(j => j.TransportID.Equals(searchParam)).ToList();
groupedList 结果:
transport1
transport2
...
transportN
filteredlList 结果:
transport1 JournalDate1 Brand Model
transport2 JournalDate1 Brand Model
transport1 JournalDate2 Brand Model
transport2 JournalDate2 Brand Model
...
从这两个列表中,我需要一个结构如下:
分组列表
+过滤列表
transport1 Brand Model
journal1 JournalDate
journal2 JournalDate
...
journalN JournalDate
transport2 Brand Model
journal1 JournalDate
journal2 JournalDate
...
journalN JournalDate
...
transportN Brand Model
journal1 JournalDate
journal2 JournalDate
...
journalN JournalDate
为了获取包含一些数据的 GroupedList,我这样做了:
GroupedlList = new List<JournalGrouped>();
foreach (var t in groupedList)
{
Transport transportDetail = DBContext.Transports.Where(tr => tr.TransportID.Equals(t.Key.TransportID)).FirstOrDefault();
JournalGrouped journalTransport = new JournalGrouped
{
TransportID = transportDetail.TransportID,
Brand = transportDetail.BrandName,
Model = transportDetail.ModelName,
**TransportJournal** = new List<Journal>()
};
}
我不知道如何获取按 TransportID 过滤的 TransportJournal 列表
这将是我的 FilteredList 作为我的 GroupedList
的详细描述符
(如果我通过添加过滤器 TransportID 直接从 Journal 构造它,一切正常)我得到两个列表,将它们放在我的表单中的两个不同的网格中,一切看起来都不错,但我想要像我描述的那样的列表结构以上
XAML 代码如下所示:
<dxg:GridControl x:Name="grid" ItemsSource="{Binding Path=GroupedJournalList}"
<dxg:GridControl.View>
<dxg:TableView ShowGroupPanel="False" ShowSearchPanelMode="Never" AllowGrouping="False"/>
</dxg:GridControl.View>
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="Brand"/>
<dxg:GridColumn FieldName="Model"/>
</dxg:GridControl.Columns>
<dxg:GridControl.DetailDescriptor>
<dxg:DataControlDetailDescriptor ItemsSourceBinding="{Binding TransportJournal}" ShowHeader="True">
<dxg:GridControl>
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="JournalDate"/>
</dxg:GridControl.Columns>
</dxg:GridControl>
</dxg:DataControlDetailDescriptor>
</dxg:GridControl.DetailDescriptor>
</dxg:GridControl>
您可以使用 MultiBindingPropery 获取 currentSlected {传输和日志}
<MultiBinding Converter="{StaticResource "FileterName"}">
<Binding Path="CurentColumnFilter" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}" Mode="TwoWay" />
<Binding Path="TransactionTypes" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}" Mode="TwoWay"/>
更多可以参考这个link
https://www.devexpress.com/Support/Center/Question/Details/Q491120
我正在使用 Visual Studio 2015 C#,WPF/MVVM,Devexpress, EntityFramework6 和代码优先到 Oracle 数据库。
我的数据库(传输和日志)中有两个 table,我需要从日志 table 中按 TransportID 获取分组的日志列表,然后将传输详细信息添加到此列表,然后创建具有这两个列表的 GridControl。你能指导我吗,我做错了什么
首先我创建了实体:
public partial class Transport
{
[Key]
public int TransportID { get; set; }
public string Brand { get; set; }
public string Model { get; set; }
public class Journal
{
[Key]
public int JournalID { get; set; }
public int TransportID { get; set; }
[ForeignKey("TransportID")]
public virtual Transport Transport { get; set; }
public DateTime JournalDate { get; set; }
}
public class JournalGrouped
{
public int JournalID { get; set; }
public int TransportID { get; set; }
public string Brand { get; set; }
public string Model { get; set; }
public DateTime JournalDate { get; set; }
public List<Journal> TransportJournal { get; set; }
}
然后在 ViewModel 中构建列表:
var journal = dbContext.Journals.AsQueryable();
var groupedList = journal.GroupBy(j => new { j.TransportID}).ToList();
var filteredList = journal.Where(j => j.TransportID.Equals(searchParam)).ToList();
groupedList 结果:
transport1
transport2
...
transportN
filteredlList 结果:
transport1 JournalDate1 Brand Model
transport2 JournalDate1 Brand Model
transport1 JournalDate2 Brand Model
transport2 JournalDate2 Brand Model
...
从这两个列表中,我需要一个结构如下:
分组列表 +过滤列表
transport1 Brand Model
journal1 JournalDate
journal2 JournalDate
...
journalN JournalDate
transport2 Brand Model
journal1 JournalDate
journal2 JournalDate
...
journalN JournalDate
...
transportN Brand Model
journal1 JournalDate
journal2 JournalDate
...
journalN JournalDate
为了获取包含一些数据的 GroupedList,我这样做了:
GroupedlList = new List<JournalGrouped>();
foreach (var t in groupedList)
{
Transport transportDetail = DBContext.Transports.Where(tr => tr.TransportID.Equals(t.Key.TransportID)).FirstOrDefault();
JournalGrouped journalTransport = new JournalGrouped
{
TransportID = transportDetail.TransportID,
Brand = transportDetail.BrandName,
Model = transportDetail.ModelName,
**TransportJournal** = new List<Journal>()
};
}
我不知道如何获取按 TransportID 过滤的 TransportJournal 列表 这将是我的 FilteredList 作为我的 GroupedList
的详细描述符(如果我通过添加过滤器 TransportID 直接从 Journal 构造它,一切正常)我得到两个列表,将它们放在我的表单中的两个不同的网格中,一切看起来都不错,但我想要像我描述的那样的列表结构以上
XAML 代码如下所示:
<dxg:GridControl x:Name="grid" ItemsSource="{Binding Path=GroupedJournalList}"
<dxg:GridControl.View>
<dxg:TableView ShowGroupPanel="False" ShowSearchPanelMode="Never" AllowGrouping="False"/>
</dxg:GridControl.View>
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="Brand"/>
<dxg:GridColumn FieldName="Model"/>
</dxg:GridControl.Columns>
<dxg:GridControl.DetailDescriptor>
<dxg:DataControlDetailDescriptor ItemsSourceBinding="{Binding TransportJournal}" ShowHeader="True">
<dxg:GridControl>
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="JournalDate"/>
</dxg:GridControl.Columns>
</dxg:GridControl>
</dxg:DataControlDetailDescriptor>
</dxg:GridControl.DetailDescriptor>
</dxg:GridControl>
您可以使用 MultiBindingPropery 获取 currentSlected {传输和日志}
<MultiBinding Converter="{StaticResource "FileterName"}">
<Binding Path="CurentColumnFilter" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}" Mode="TwoWay" />
<Binding Path="TransactionTypes" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}" Mode="TwoWay"/>
更多可以参考这个link https://www.devexpress.com/Support/Center/Question/Details/Q491120