2013-05-07 88 views
2

我有一個TreeView在WPFLINQ查詢 - 在WPF C#一個查詢

工作履行期望的結果我有我使用填充一個TreeView的兩級兩類。

第一個問題是我可以用一個查詢來做到這一點,或者比我在下面做到的更好。

主要問題是我想包括h.ItemIdViewDocItems中的其他列,並仍然在樹視圖中實現層次結構。

現在TitleDocHistory類是樹的根級別。

BaseNumberBaseHistory是第二級。

 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

回答

3

如果你可以讓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); 
}