ObjectDataSource 和 LINQ 技术细节

发布于 2024-11-02 23:51:19 字数 861 浏览 0 评论 0原文

我有一个关于 LINQ 的小问题。

我有一个类别集合(大约 100 个)。其中一些是其他类别的孩子。这是此类型的 IList

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

如果我想在 DataList(可能是 DataList)中表示此类型,最好的方法是什么code>DataList、DataList 等)使用 ASP.net。另一个技术问题是孩子的数量不是静态的。我们目前有 3 个深度,但很容易有 5 个。

必须说我无法更改数据返回的位置和方式,因为它是来自外部 API 的方式。我要做的就是向他们展示。

谢谢您,请随时向我询问更多详细信息。我来自魁北克,通常说法语,所以也许我的英语不是很好。

编辑
必须告诉我,我知道如何对 DataList 进行数据绑定。我的问题实际上是如何将数据列表的数据列表(并且不知道深度)数据绑定到包含所有级别的简单维度列表。是否可以?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山色无中 2024-11-09 23:51:19

为了子孙后代......
列表<类别>类别应该没问题。属性通过以下方式公开:

categories[0].TempID

您可以针对集合运行 LINQ:

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

UPDATE_3:

好的,我现在明白了 - 您收到一个如下所示的对象:

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

...并且您需要能够添加集合子类别?

如果您无法继承该类,我将通过以下方式使用该对象:

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

...在外部,调用:

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

    return myCategory.GotChildren;
}  

更新该方法以与 Dictionary.TryGetValue() 类似的方式工作;

UPDATE_2:

您需要向您的类型添加集合(ICollectionList 等)属性:

 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

这将添加无限的深度。我不明白为什么不能使用 DataList 作为您的 SubItems 数据类型。

更新:

仔细查看 DataList 类后,您必须使用 ICollection 作为数据源。 MSDN 显示 List 继承自 ICollection & ICollectionIList 继承自ICollection;而 IList 继承自 ICollection

MSDN 还有示例

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文