LINQ orderby int 数组索引值

发布于 2024-10-08 07:40:07 字数 717 浏览 1 评论 0原文

使用 LINQ 我想按传入的 int 数组索引进行排序。

所以在下面的代码中 attribueIds 是我的 int 数组。我将该数组中的整数用于 where 子句,但我希望结果按照它们在数组中的顺序排列。

public List BuildTable(int[] attributeIds)
{
    using (var dc = new MyDC())
    {
        var ordering = attributeIds.ToList();

        var query = from att in dc.DC.Ecs_TblAttributes
                    where attributeIds.Contains(att.ID)
                    orderby(ordering.IndexOf(att.ID))
                    select new Common.Models.Attribute
                    {
                        AttributeId = att.ID,
                        DisplayName = att.DisplayName,
                        AttributeName = att.Name
                    };

        return query.ToList();
    }
}

Using LINQ I would like to sort by the passed in int arrays index.

So in the code below attribueIds is my int array. I'm using the integers in that array for the where clause but I would like the results in the order that they were in while in the array.

public List BuildTable(int[] attributeIds)
{
    using (var dc = new MyDC())
    {
        var ordering = attributeIds.ToList();

        var query = from att in dc.DC.Ecs_TblAttributes
                    where attributeIds.Contains(att.ID)
                    orderby(ordering.IndexOf(att.ID))
                    select new Common.Models.Attribute
                    {
                        AttributeId = att.ID,
                        DisplayName = att.DisplayName,
                        AttributeName = att.Name
                    };

        return query.ToList();
    }
}

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

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

发布评论

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

评论(2

記柔刀 2024-10-15 07:40:07

我建议从 attributeIDs 数组中进行选择。这将确保您的物品能够正确排序,而无需排序。

代码应该是这样的:

var query = 
from id in attributeIds
let att = dc.DC.Ecs_TblAttributes.FirstOrDefault(a => a.ID == id)
where att != null
select new Common.Models.Attribute
{
    AttributeId = att.ID,
    DisplayName = att.DisplayName,
    AttributeName = att.Name
};

I would recommend selecting from the attributeIDs array instead. This will ensure that your items will be correctly ordered without requiring a sort.

The code should go something like this:

var query = 
from id in attributeIds
let att = dc.DC.Ecs_TblAttributes.FirstOrDefault(a => a.ID == id)
where att != null
select new Common.Models.Attribute
{
    AttributeId = att.ID,
    DisplayName = att.DisplayName,
    AttributeName = att.Name
};
千柳 2024-10-15 07:40:07

你为什么不加入:

public List BuildTable(int[] attributeIds)
{
    using (var dc = new MyDC())
    {
        var query = from attID in attributeIds
                    join att in dc.DC.Ecs_TblAttributes
                    on attID equals att.ID
                    select new Common.Models.Attribute
                           {
                               AttributeId = attID,
                               DisplayName = att.DisplayName,
                               AttributeName = att.Name
                           };
         return query.ToList();
    }
}

Why don't you join:

public List BuildTable(int[] attributeIds)
{
    using (var dc = new MyDC())
    {
        var query = from attID in attributeIds
                    join att in dc.DC.Ecs_TblAttributes
                    on attID equals att.ID
                    select new Common.Models.Attribute
                           {
                               AttributeId = attID,
                               DisplayName = att.DisplayName,
                               AttributeName = att.Name
                           };
         return query.ToList();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文