开发者

ObjectDataSource and LINQ technicality

I've a little question on LINQ.

I have a collection of categories (about 100). Some of them are children of another category. This is an IList of this type:

 public Guid TempID { get; set; }
 public Nullable<int> ID { get; set; }
 public bool Published { get; set; }
 public int CategoryLevel { get; set; }
 public int CategoryOrder { get; set; }
 public DateTime UpdatedDate { get; set; }
 public Nullable<int> RefParent { get; set; }
 public bool GotChildren { get; set; }

What is the best approach if I want to represent this type in a DataList (potentially a DataList of DataList, of DataList, etc)开发者_运维问答 using ASP.net. Another technicality is that the number of children is not static. We presently have 3 depth but it can easly have 5.

Must say that I can't change where and how data are return because it's how it comes from an external API. What I've to do, it's simply showing them.

Thank you, and feel free to ask me more details. I'm from quebec and usualy talk french so maybe me english is not very good.

Edit

Must tell that I know how to databind the DataList. My problem is realy how can I DataBind a datalist of datalist (and don't know the depth) to a Simple dimension list that contains all level. Is it possible?


for posterity's sake...
List<Category> categories should be fine. Properties are exposed via something like :

categories[0].TempID

You can run LINQ against the collection:

var catIDs = from cat in categories
             where cat.ID.HasValue == true
             select cat;

UPDATE_3:

Okay, I understand now - you receive an object that looks like:

 public Guid TempID { get; set; }
 public Nullable<int> ID { get; set; }
 public bool Published { get; set; }
 public int CategoryLevel { get; set; }
 public int CategoryOrder { get; set; }
 public DateTime UpdatedDate { get; set; }
 public Nullable<int> RefParent { get; set; }
 public bool GotChildren { get; set; }

...and you need to be able to add collections of child categories?

If you cannot inherit the class, I would consume the object by:

class MyCategory
{
    // add fields for respective Category properties
    // or just the following
    Category category;
    List<Category> subCategories;

    MyCategory (Category category)
    {
        this.category = category;

        if (this.category.GotChildren)
        {
            // query TempIDs, Level, etc. from collection of Categories
            // and assign to subCategories collection
        }
    }

    // add property getters/setters as needed
    public List<Category> SubCategories
    { get { return subCategories; } }
}

...externally, call:

bool TryGetSubCategories (MyCategory myCategory, out DataList myDataList)
{
    if (myCategory.GotChildren)
    {myDataList.DataSource = myCategory.SubCategories;}

    return myCategory.GotChildren;
}  

Updated the method to work in similar fashion to Dictionary.TryGetValue();

UPDATE_2:

You will need to add a collection (ICollection, List<T>, etc.) property to your type:

 public Guid TempID { get; set; }
 public Nullable<int> ID { get; set; }
 public bool Published { get; set; }
 public int CategoryLevel { get; set; }
 public int CategoryOrder { get; set; }
 public DateTime UpdatedDate { get; set; }
 public Nullable<int> RefParent { get; set; }
 public bool GotChildren { get; set; }
 public List<Categories> SubCategories {get; set;} // add this

This will add unlimited depth. I don't see why couldn't use the DataList as your SubItems data type.

UPDATE:

After looking at the DataList class more carefully, you must use an ICollection as the DataSource. MSDN shows that List<T> inherits from ICollection & ICollection<T>. IList inherits from ICollection; while IList<T> inherits from ICollection<T>.

MSDN also has a sample.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜