Say I have a list of string
List<string> lst=new List<string>(new string[]{"a","b","c","d"});
I wish to get element from index 0 to index 2 assign it to another List lst1(i.e. the element of lst is {"a","b"}), then remove it from lst (i.e. lst becomes {"c","d"}, what's the quickest way of doing this? I am thinking is there any command like
List<string> lst=new List<string>();
lst1=lst.getElements(1,2);
lst.remove(1,2);
Use GetRange() to copy of a range of elements and RemoveRange() to removes a range of elements.
Example :
List<string> lst = new List<string>(new string[] { "a", "b", "c", "d" });
List<string> lst1 = lst.GetRange(0, 2);
lst.RemoveRange(0, 2);
Good Luck !!
You can use AddRange and RemoveRange:
var range = lst.Take(2);
lst1.AddRange(range);
lst.RemoveRange(0, 2);
Demo: http://ideone.com/1X2cV
Related
My idea is to use a new list (List1) and compare it with another list (List2) and create a new list (List3) that exclude all common elements in both lists and results on the non common elements. The difficult thing (to me) is that List1 and List2 elements are not a true match. List1 elements might be part of List2 elements, but not a truly match. Using exclude does not seem to allow the use of IndexOf to compare the two list elements.
Does anyone have an idea how to achieve this?
Thanks in advance.
Assuming you have List1 and List2. Below is the simplest way to compare elements in two lists.
IList<string> List3 = new List<string>();
foreach (var item1 in List1)
{
foreach(var item2 in List3)
{
if (item1 == item2)
{
List3.Add(item1);
}
}
}
My idea is to use a new list (List1) and compare it with another list
(List2) and create a new list (List3) that exclude all common elements
in both lists and results on the non common elements.
From Comments
I need to compare each element in both lists List1 element exists in
List2 element (both strings).
One of the easiest ways to find unique from two lists
var List1 = new List<string>() { "a", "b", "c", "d" };
var List2 = new List<string>() { "a", "e", "f", "g", "c","z" };
var List3 = new List<string>();
List3.AddRange(List1.Except(List2));
List3.AddRange(List2.Except(List1));
List3.ForEach(l=>Console.WriteLine(l));
How about this:
List commonElements = new List<string>();
foreach (var smallString in SmallList)
{
if (large.Any(x => x.Contains(smallString)))
{
// Add to common elements
commonElements.Add(smallString);
}
}
I have got a simple question I am having a list:
List<string> test = new List<string> {"one", "two", "three", "four"}
Now I want to take for example value "three" and get all elements after it, so it would be looking like:
List<string> test = new List<string> {"three", "four"}
But we do not know where list end so it can be list of many elements and we can not define end as const.
Is it possible?
It sounds like you're looking for SkipWhile from LINQ:
test = test.SkipWhile(x => x != "three").ToList();
That will skip everything until (but not including) the "three" value, then include everything else. It then converts it to a list again.
Since you assign the filtered list back to initial one, then just remove first items up to "three" one:
int count = test.IndexOf("three");
test.RemoveRange(0, count < 0 ? test.Count : count);
This implementation doesn't create additional list, but modifies existing one.
This might do the trick for you
var list2 = test.Skip(2).Take(test.Count).ToList();
or better
var list3 = test.Skip(2).ToList();
Without LINQ it could be done something like this
List<string> outtest = new List<string>();
bool drty = false;
foreach(string st in test)
{
if(st == "three") //or whatever is the input.
drty = true;
if(drty)
outtest.Add(st);
}
i have a nested list that contains a set of lists, some of these lists are duplicated, i wanna just make a second list without duplicated lists. i tried this :
List<List<string>> liste1 = new List<List<string>>();
List<List<string>> liste2 = new List<List<string>>();
List<string> l1 = new List<string> { "a", "b", "c" };
List<string> l2 = new List<string> { "h", "x", "g" };
List<string> l3 = new List<string> { "a", "b", "c" };
List<string> l4 = new List<string> { "z", "t", "n" };
liste1.Add(l1);
liste1.Add(l2);
liste1.Add(l3);
liste1.Add(l4);
foreach (List<string> lis in liste1)
{
if(!liste2.Contains(lis))
{
liste2.Add(lis);
}
}
it seems easy but its not working, any help will be appreciated. Thx.
Using Linq, you could achieve this.
You could take help of extension methods and look for SequentialEqual of two lists. If the order is not important use Except extension (something like ...s.Except(x).Any()).
var liste2= liste1.Where((x,i)=> !liste1.Skip(i+1).Any(s=>s.SequenceEqual(x)));
Check this Demo
You are checking for reference equality. Instead of using Contains try this substitution
//if (!liste2.Contains(lis))
if(!liste2.Any(subList => subList.SequenceEqual(lis)))
SequenceEqual is an extension method on IEnumerable<T>. I think you will need a using statement importing the System.Linq namespace.
If you dont want to test that the child lists are not sequence-equal, but set-equal (i.e. order is not important), then consider using an implementation of ISet<T> like HashSet<int> instead of List<int>.
I have two list my requirement is I want to create one list whose data should be all value of x minus all value from y.
For example.
List<string> s = new List<string>();
List<string> y = new List<string>();
List s contain {“a”,”b”,”c”,”d”}
List y contain {“b”,”c”}
I want one to create one more list
List<string> z = new List<string>();
Whose value should be
{“a”,”d”}
Just use LinQ:
z=s.Except(y).ToList();
Here's a reference for your specific problem: How to: Find the Set Difference Between Two Lists (LINQ)
One approach it would be the following
z=s.Where(x=>!y.Contains(x)).ToList();
this should do it
List<string> a = new List<string> { "a", "b", "c", "d" };
List<string> b = new List<string> { "b", "c" };
List<string> c = a.Except(b, StringComparer.OrdinalIgnoreCase).ToList();
How do I check whether a List contains an element that exists in another List using LINQ in C#? I don't want to use a for/while loop.
So, if List1 has A, B, C and List2 has B, 1, 2, then I would return true.
Try this:
List<string> a = ...
List<string> b = ...
var inComon = a.Intersect(b).Any();
Use Enumerable.Any Method:
List<string> l1 = new List<string> { "1", "2" };
List<string> l2 = new List<string> { "1", "3" };
var result = l2.Any(s => l1.Contains(s));
I'd say the Intersect method (see answer by dasblinkenlight) + Any must work better than Contains + Any. It is definetely better to use Any than Count.