简体   繁体   English

如何在c#中移动列表中的项目子集?

[英]How to move a subset of items in a list in c#?

I have the following list:我有以下清单:

List<string> l=new List<string()>{"a","b","c","d","1","2","3","4","a1","b2","c3","d4"};

And I have oldIndex , size and newIndex .我有oldIndexsizenewIndex

Let's say for the above list oldIndex = 4 , size = 4 , newIndex = 0 .假设上面的列表oldIndex = 4 , size = 4 , newIndex = 0

The output list I would like is:我想要的输出列表是:

{"1","2","3","4","a","b","c","d","a1","b2","c3","d4"};

The solution I've found is(which is not working for all cases):我找到的解决方案是(不适用于所有情况):

for(int i=0;i<size;i++)
{
    if(oldIndex<newIndex)
    {
        var x=l[oldIndex];
        l.RemoveAt(oldIndex);
        l.Insert(newIndex+size-1,x);
    }
    else
    {
        var x=l[oldIndex+i];
        l.RemoveAt(oldIndex+i);
        l.Insert(newIndex+i,x);
    }
}

Is there any better way to this, perhaps using LINQ or something better, or is the above the only way to this?有没有更好的方法,也许使用 LINQ 或更好的方法,或者以上是唯一的方法?

I found the above answer from this link.我从这个链接找到了上面的答案。 The difference in this question is that I would like to move a block, and not just single element.这个问题的不同之处在于我想移动一个块,而不仅仅是单个元素。

You can use the following for this:您可以为此使用以下内容:

List<string> removedRange = l.GetRange(oldIndex, size);
l.RemoveRange(oldIndex, size);
l.InsertRange(newIndex, removedRange);

There are many different ways to do this.有许多不同的方法可以做到这一点。

Raviraj Palvankar's answer is probably the simplest, but to complement it (and since you specifically mentioned linq), here are a couple of ( admittedly somewhat over-engineered ) alternatives. Raviraj Palvankar 的答案可能是最简单的,但为了补充它(并且因为您特别提到了 linq),这里有几个(诚然有些过度设计的)替代方案。

List<string> l = new List<string>{"a","b","c","d","1","2","3","4","a1","b2","c3","d4"};

var oldIndex = 4;
var size= 4;
var newIndex = 0;

To move:移动:

var toMove = l.Skip(oldIndex).Take(size).ToList();
var newList = l.Take(oldIndex).ToList().Concat(l.Skip(oldIndex + size)).ToList();

newList.InsertRange(newIndex, toMove);

If you're able to move the section {"a","b","c","d"} instead (requires knowing a different new index, and does not require oldIndex , since it will be 0), then the move-operation itself becomes easier:如果您能够移动{"a","b","c","d"} (需要知道不同的新索引,并且不需要oldIndex ,因为它将是 0),那么移动操作本身变得更容易:

var newIndex = 4; // Replace previous value which was 0
var size= 4; // Elements a - d

var newList = l.Skip(size).ToList();
newList.InsertRange(newIndex, l.Take(size));

...and yet another way (which may actually be the simplest to read ): ...还有另一种方式(实际上可能是最简单的阅读方式):

var oldIndex = 4;
var size= 4;

var toMove = l.Skip(oldIndex).Take(size).ToList();
var firstPart = l.Take(oldIndex);
var lastPart = l.Skip(oldIndex + size);
var combined = toMove.Concat(firstPart).Concat(lastPart);

C#列表<object> .RemoveAll() - 如何删除列表的子集?<div id="text_translate"><p> 我有两个具有多个匹配属性的<strong>feeds_Auto</strong>和<strong>Product</strong>类。 对于这个特殊问题,AutoID 是我需要使用的唯一字段。</p><p> 我有一个包含数百个<em>唯一</em>条目的List&lt;FeedsAuto&gt; 。 我有一个小List&lt;Product&gt;有 10、20 个<em>唯一</em>条目。 我现在想从大列表中删除小列表中的所有项目。</p><p> 如何使用 RemoveAll({lambda expression}) 来完成此操作? 我发现的所有示例都依赖于简单类型(字符串、整数等)的通用列表。</p><pre> private static int DoInserts(ref List&lt;Model.feeds_Auto&gt; source, ref List&lt;Model.Product&gt; target, Guid companyID) { List&lt;Model.Product&gt; newProductList = new List&lt;Model.Product&gt;(); List&lt;Model.Product&gt; dropSourceList = new List&lt;Model.Product&gt;(); using (var db = Helpers.GetProdDB()) { foreach (var src in source) { var tgt = target.Where(a =&gt; a.alternateProductID == src.AutoID &amp;&amp; a.LastFeedUpdate &lt; src.DateModified).FirstOrDefault(); if (tgt == null) { newProductList.Add(new Model.Product{...}); dropSourceList.Add(src); } } db.SaveChanges(); if (dropSourceList.Count &gt; 0) { source.RemoveAll(????); } } }</pre><p> 在循环中执行此操作并不困难:</p><pre> foreach (var drop in dropSourceList) { source.RemoveAll(a =&gt; a.AutoID == drop.AutoID); }</pre><p> (对我来说)循环一个集合以在每次传递中为一个项目调用 RemoveAll 只是没有意义。</p></div></object> - C# List<object>.RemoveAll() - How to remove a subset of the list?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在控制台C#中的列表中移动光标? - How to move cursor for items in List in console C#? 在C#中的列表框之间移动项目 - Move items between list boxes in C# 如何在C#中将所选项目从一个C1List移动​​到另一个? - How to move move selected items from one C1List to another in C#? 如何将项目从列表移动到C#中的另一个列表? - How do I move items from a list to another list in C#? C# 在不改变相对位置的情况下按列表顺序移动项目 - C# Move Items In List Order Without Changing Relative Positions 在 C# 中将列表中的所有项目移动一个 position 的最佳方法 - Best way to move all items in a list by one position in C# 如何按最小到最大顺序获取项目并移动到临时列表 C# - How to get items in order by min to max and move to temp list C# C#列表<object> .RemoveAll() - 如何删除列表的子集?<div id="text_translate"><p> 我有两个具有多个匹配属性的<strong>feeds_Auto</strong>和<strong>Product</strong>类。 对于这个特殊问题,AutoID 是我需要使用的唯一字段。</p><p> 我有一个包含数百个<em>唯一</em>条目的List&lt;FeedsAuto&gt; 。 我有一个小List&lt;Product&gt;有 10、20 个<em>唯一</em>条目。 我现在想从大列表中删除小列表中的所有项目。</p><p> 如何使用 RemoveAll({lambda expression}) 来完成此操作? 我发现的所有示例都依赖于简单类型(字符串、整数等)的通用列表。</p><pre> private static int DoInserts(ref List&lt;Model.feeds_Auto&gt; source, ref List&lt;Model.Product&gt; target, Guid companyID) { List&lt;Model.Product&gt; newProductList = new List&lt;Model.Product&gt;(); List&lt;Model.Product&gt; dropSourceList = new List&lt;Model.Product&gt;(); using (var db = Helpers.GetProdDB()) { foreach (var src in source) { var tgt = target.Where(a =&gt; a.alternateProductID == src.AutoID &amp;&amp; a.LastFeedUpdate &lt; src.DateModified).FirstOrDefault(); if (tgt == null) { newProductList.Add(new Model.Product{...}); dropSourceList.Add(src); } } db.SaveChanges(); if (dropSourceList.Count &gt; 0) { source.RemoveAll(????); } } }</pre><p> 在循环中执行此操作并不困难:</p><pre> foreach (var drop in dropSourceList) { source.RemoveAll(a =&gt; a.AutoID == drop.AutoID); }</pre><p> (对我来说)循环一个集合以在每次传递中为一个项目调用 RemoveAll 只是没有意义。</p></div></object> - C# List<object>.RemoveAll() - How to remove a subset of the list? 如何将 obj 移动到 c# 中的列表顶部 - how to move obj to the top of list in c# 如何将列表项设置为null C#? - How to set list items to null C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM