Linq查询——在WPF c#中通过一个查询执行预期的结果
本文关键字:查询 执行 一个 结果 WPF Linq | 更新日期: 2023-09-27 18:12:27
我正在使用WPF中的树视图
我有两个类,我正在使用填充树视图的两个级别。
第一个问题是我可以用一个查询或任何比我在下面实现的更好的查询来做到这一点。
主要问题是我想包括h.ItemId
和ViewDocItems
中的其他列,并且仍然在树视图中实现层次结构。
现在DocHistory
类中的Title
是树的根级。
, BaseHistory
中的BaseNumber
是第二层。
public class DocHistory
{
private ObservableCollection<BaseHistory> mBaseHis = new ObservableCollection<BaseHistory>();
public Guid Id { get; set; }
public Guid ClassId { get; set; }
public string Title { get; set; }
public ObservableCollection<BaseHistory> BaseHis { get { return mBaseline; } }
}
public class BaseHistory
{
public Guid BaseId { get; set; }
public string BaseName { get; set; }
public int BaseNumber { get; set; }
public bool BaseFinal { get; set; }
}
//this guid is only for testing
mGuid = new Guid("0497F3DA-AE3E-40C7-AF91-9B26EEE2A437");
var query = (from d in mContext.Docs
orderby d.Title ascending
join b in mContext.Base on d.DocId equals b.DocId
join h in mContext.ViewDocItems on b.BaseId equals h.BaseId
where h.ItemId == mGuid
select d).Distinct();
List<Database.Doc> DocList = query.ToList();
foreach (Database.Doc result in DocList)
{
DocHistory newHistory = new DocuHistory() { Id = result.DocId, Title = result.Title, ClassId = result.ClassId };
mHistory.Add(newHistory);
var documents = from d in result.Base
orderby d.LineNumber
select new BaseHistory()
{
BaseId = d.BaseId,
Name = d.Title,
BaseFinal = d.Final.Value,
LineNumber = d.LineNumber.Value
};
documents.ToList().ForEach(d => newHistory.BaseHis.Add(d));
}
这是我要绑定到
的属性 private ObservableCollection<DocHistory> mHistory = new ObservableCollection<DocHistory>();
public ObservableCollection<DocHistory> DocsHistory
{
get
{
return mHistory;
}
}
我使用ItemsSource="{Binding Path=DocsHistory}"
为treeView
和Hierarchial DataTemplate
绑定到BaseHis
如果您可以让BaseHis
属性为IEnumerable
,您可以这样做:
var results =
(from d in mContext.Docs
orderby d.Title ascending
join b in mContext.Base on d.DocId equals b.DocId
join h in mContext.ViewDocItems on b.BaseId equals h.BaseId
where h.ItemId == mGuid
select new DocHistory
{
Id = d.DocId,
Title = d.Title,
ClassId = d.ClassId,
BaseHis =
from b in d.Base
orderby b.LineNumber
select new BaseHistory
{
BaseId = d.BaseId,
Name = d.Title,
BaseFinal = d.Final.Value,
LineNumber = d.LineNumber.Value
}
});
foreach(var r in results)
{
mHistory.Add(r);
} // or mHistory.AddRange(results)
即使你不能改变BaseHis
的数据类型,你也可以用一个匿名类做到这一点:
var results =
(from d in mContext.Docs
orderby d.Title ascending
join b in mContext.Base on d.DocId equals b.DocId
join h in mContext.ViewDocItems on b.BaseId equals h.BaseId
where h.ItemId == mGuid
select new
{
Id = d.DocId,
Title = d.Title,
ClassId = d.ClassId,
BaseHis =
from b in d.Base
orderby b.LineNumber
select new BaseHistory
{
BaseId = d.BaseId,
Name = d.Title,
BaseFinal = d.Final.Value,
LineNumber = d.LineNumber.Value
}
});
foreach(var r in results)
{
var hist = new DocHistory()
{
Id = r.Id,
Title = r.Title,
ClassId = d.ClassId;
};
foreach(var h in r.BaseHis)
{
hist.BaseHis.Add(h);
}
mHistory.Add(r);
}