C#上/下移动项目 [英] C# moving an item up/down

查看:89
本文介绍了C#上/下移动项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含物品的清单.他们都有一个排序"列.排序列的类型为int,并且是唯一的.

I got a list that contains items. They all got a 'Sort' column. The sort column is of type int, and it's unique.

场景:

排序1;排序2;排序3;

sort 1; sort 2; sort 3;

如果用户在列表中将某个项目上移(例如,排序3)(例如,移至位置1,其排序值将为1),则在刚上移的项目下的项目必须在列表中向下移动,应相应地应用排序编号.在这种情况下,所有转移的项目都会排序-1.

If the user moves an item up (for example sort 3) in the list (for example to position 1, which would give the sort value 1), the items that are under the one that just got moved up, have to be shifted down in the list, and the sort number should be applied accordingly. In this case all shifted items sort - 1.

因此,方案的最终状态如下:

So the end state of the scenario looks like this:

排序1为排序3;第3类是第2类;排序3现在是排序1;

sort 1 was sort 3; sort 3 was sort 2; sort 3 is now sort 1;

我如何使用LINQ来做到这一点? 这不仅仅是3个项目.可能会更多.

How can i do this with LINQ? It's not just 3 items. It can be a lot more.

 public ActionResult Up(int id)
 {
    var item = dataContext.item.FirstOrDefault(x => x.item == id);

    return View(dataContext.items);
 }

推荐答案

这可能不是最容易理解的代码,但我已经对其进行了测试,并且似乎可以正常工作.

That might not be the easiest code to understand but I've tested it and it seems to work as intended.

让我们设置一些数据.

var array = new [] 
{ 
    new { Sort = 1, Value = "foo1", },
    new { Sort = 2, Value = "foo2", },
    new { Sort = 3, Value = "foo3", },
    new { Sort = 4, Value = "foo4", },
};

var oldSort = 1;
var newSort = 3;

首先,根据新旧索引的位置将查询分为三部分,因此我们可以分别处理每种情况.

First, query is split into three parts depending on positions of old and new indexes, so we can handle each case separately.

var q = 
    oldSort > newSort ? 
        array
            .Where(x => x.Sort >= newSort && x.Sort < oldSort)
            .Select(x => new { Sort = x.Sort + 1, Value = x.Value })
            .Union(array.Where(x => x.Sort < newSort || x.Sort > oldSort))
            .Union(array.Where(x => x.Sort == oldSort)
                        .Select(x => new { Sort = newSort, Value = x.Value }))
            : 
    oldSort < newSort ?         
        array
            .Where(x => x.Sort <= newSort && x.Sort > oldSort)
            .Select(x => new { Sort = x.Sort - 1, Value = x.Value })
            .Union(array.Where(x => x.Sort > newSort || x.Sort < oldSort))
            .Union(array.Where(x => x.Sort == oldSort)
                        .Select(x => new { Sort = newSort, Value = x.Value }))
            :
    array;

下移项目(oldSort = 1newSort = 3)的结果:

Results for moving an item down (oldSort = 1, newSort = 3):

1 foo2
2 foo3 
3 foo1 
4 foo4 

向上移动项目(oldSort = 4newSort = 2)的结果:

Results for moving an item up (oldSort = 4, newSort = 2):

1 foo1 
2 foo4 
3 foo2 
4 foo3 

更新:该查询通过将序列分为三部分来工作

UPDATE: The query works by splitting a sequence into three parts

  • 具有旧索引的项目变为具有新索引的项目;
  • 新旧索引之间的项目上下移动;
  • 其余的保持索引.

结果是零件的并集.

更新2 :该查询适用于任意数量的项目,并且故意没有循环.

UPDATE 2: The query works for any number of items and the absence of loops is intentional.

更新3 :这是使查询与LINQ-to-Entities一起工作的一种方法.

UPDATE 3: Here's one way to make the query work with LINQ-to-Entities.

using (var context = new TestDBEntities())
{
    var array = context.TestTables;
    var q =
        oldSort > newSort ?
            array
                .Where(x => x.Sort >= newSort && x.Sort < oldSort)
                .Select(x => new { Sort = x.Sort + 1, Value = x.Value })
                .Union(array.Where(x => x.Sort < newSort || x.Sort > oldSort)
                            .Select(x => new { Sort = x.Sort, Value = x.Value }))
                .Union(array.Where(x => x.Sort == oldSort)
                            .Select(x => new { Sort = newSort, Value = x.Value }))
                :
        oldSort < newSort ?
            array
                .Where(x => x.Sort <= newSort && x.Sort > oldSort)
                .Select(x => new { Sort = x.Sort - 1, Value = x.Value })
                .Union(array.Where(x => x.Sort > newSort || x.Sort < oldSort)
                            .Select(x => new { Sort = x.Sort, Value = x.Value }))
                .Union(array.Where(x => x.Sort == oldSort)
                            .Select(x => new { Sort = newSort, Value = x.Value }))
                :
        array.Select(x => new { Sort = x.Sort, Value = x.Value });
}

区别在于类型现在是显式兼容的.

The difference is that the types are now explicitly compatible.

这篇关于C#上/下移动项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆