2

I am working with a treeview in WPF

I have two classes that I am using to populate two levels of a treeview.

The first question is can I do this with one query or any better than I have achieved with below.

The main question is that I want to include the h.ItemId and other columns from the ViewDocItems and still achieve the hierarchy in the treeview.

Right now the Title from DocHistory class is the root level of the tree.

And BaseNumber from BaseHistory is the second level.

        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));

                    }

Here is the property I am binding to

    private ObservableCollection<DocHistory> mHistory = new ObservableCollection<DocHistory>();
    public ObservableCollection<DocHistory> DocsHistory
            {
                get
                {
                    return mHistory;
                }
            }

I am using ItemsSource="{Binding Path=DocsHistory}" for the treeView

And The Hierarchial DataTemplate is Binding to BaseHis

1 Answer 1

3

If you can let the BaseHis property be an IEnumerable, you could just do this:

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)

Even if you can't change the datatype of BaseHis, you can do this with an anonymous class:

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); 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.