C# Linq String[] list minus from String[] list - c#

Let's say
string[] admins = "aron, mike, bob";
string[] users = "mike, katie, sarah";
How can I take the users and strip out anyone from the admins.
the result should be "katie, sarah"; (mike was removed)
Is there a nice Linq way to do this?

// as you may know, this is the right method to declare arrays
string[] admins = {"aron", "mike", "bob"};
string[] users = {"mike", "katie", "sarah"};
// use "Except"
var exceptAdmins = users.Except( admins );

users.Except(admins);
See more set operations:
http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx

The easiest would to be use IEnumerable<T>.Except:
var nonAdmins = users.Except(admins);

Related

Check if a string starts with a possible number of strings in C#?

How can I most efficiently check to see if an input string starts with a string that belongs in a list of strings?
For example possiblePrefixes = "1234", "1235", "1236". If input = "1235av2425" should return true. If input = "1237352ko" should return false.
you can use Any for this. the concept here is you need to check whether there is any item in the list which is the prefix for the given string.
List<string> list = new List<string>() { "1234", "1235", "1236" };
string input = "1237352ko";
var exisits = list.Any(x => input.StartsWith(x)); //returns false
when string input = "1235av2425"; it will return true
An efficient datastructure for this type of search would be a prefix tree (aka "Trie").
For your example data such a tree might look something like this:
123
|-4
|-5
|-6
This could allow a lookup time that is independent of the number of prefixes you want to check against.
But as far as I know there are no builtin types for this, so you would either need to find a library, or implement it yourself.
The solution using Any and StartsWith will be the best in most cases. Looking for an optimized solution will only be necessary if you have a long list of possible prefixes and/or a long list of texts to check against the same prefixes.
In that case, using a pre-compiled regular expression built once from the list of possible prefixes and then re-used for multiple checks might be a little faster.
// Build regular expression once
string[] possiblePrefixes = new string[] { "1234", "1235", "1236" };
var escaped = possiblePrefixes.Select(p => Regex.Escape(p));
string pattern = "^(" + string.Join("|", escaped) + ").*";
Regex regEx = new Regex(pattern, RegexOptions.Compiled);
// Now use it multiple times
string input = "1235av2425";
bool result = regEx.IsMatch(input);
Following are the 2 solutions
Solution # 1 (using Lambda Expression)
List<string> possiblePrefixes = new List<string>() { "1234", "1235", "1236" };
string input = "1235av2425";
var result = possiblePrefixes.Any(x => input.StartsWith(x));
Console.WriteLine(result); //returns True
Solution # 2 (using SQL)
List<string> possiblePrefixes = new List<string>() { "1234", "1235", "1236" };
string input = "1235av2425";
var result = (from val in possiblePrefixes
where input.StartsWith(val)
select val).Any();
Console.WriteLine(result); //returns True

Compare list of string with string in linq

I have list of string
Like list1 contain 'pinky','smita','Rashmi','Srivani'
And string is like
String str = "pinky, Nandini'
I want to check if neither of str present in list1,proceed further.
how to do that?
If I understood correctly, you want to return false in the example case, so you can use Any method: Check if none of the elements of the list is already in the str, here is a one liner:
if (!list.Any(x=>str.Contains(x))) ....
You can use combination of .Any() with .Contains() with !,
var list1 = new List<string>(){ "pinky", "smita", "Rashmi", "Srivani" };
string str = "pinky, Nandini";
var list2 = str.Split(",");
var nameExists = list2.Any(x => list1.Contains(x));
if(!nameExists)
{
//Your code goes here.
}
As #Fildor said, you can use Intersect(). Elegant approach,
//All credit goes to #Fildor
var nameExists = list1.Intersect(list2).Any();

How to split a server address and store in a list

I have a List<string> with some 10 strings.
The values are as follows:
\\Server\Site\MySite\File1.xml
\\Server\Site\MySite\File2.xml
\\Server\Site\MySite\File2.xml
.......................
\\Server\Site\MySite\File10.xml
I need to extract \MySIte\File1.xml to \MySite\File10.xml and store in another list.
I tried to use Split keyword, with another list to populate the splitted string. But it doesn't seem to give the correct answer.
Below is the code:
for(int index=0;index<list.Count;list++)
{
string[] myArray=list[index].Split('\\');
for(int innerIndex=0;innerIndex<myArray.Length;innerIndex++)
{
anotherList[innerIndex]=myArray[2]+"\\"+myArray[3];
}
}
Experts please help.
You don't need to work too hard if you know the input of all the strings
str.Substring(str.IndexOf("\\MySite"))
One word: LINQ!
var results = (from x in source
let parts = x.Split('\\')
select String.Join("\\", parts.Skip(1)).ToArray();
You can use following code.
List<string> source = new List<string>();
source.Add(#"\\Server\Site\MySite\File1.xml");
source.Add(#"\\Server\Site\MySite\File2.xml");
source.Add(#"\\Server\Site\MySite\File2.xml");
source.Add(#"\\Server\Site\MySite\File10.xml");
foreach(string s in source)
{
string[] parts = s.Split(new string[]{ Path.DirectorySeparatorChar.ToString() },StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine(parts[parts.Length - 1] + Path.DirectorySeparatorChar +
parts[parts.Length - 2]);
}
I would just remove anything before \MySite and get the rest:
Test data used:
List<string> source = new List<string>
{
#"\\Server\Site\MySite\File1.xml",
#"\\Server\Site\MySite\File2.xml",
#"\\Server\Site\MySite\File2.xml",
#"\\Server\Site\MySite\File10.xml",
};
query:
var result =
source
// Start removing at 0 and remove everything before '\MySite'
.Select(x => x.Remove(0, x.IndexOf("\\MySite")))
.ToList();

The ForEach Replace on this small code doesn't do what I expected it to do. What am I doing wrong here?

I don't know if this is LINQPad-related or I'm doing something wrong, but this code doesn't do what I want it to do, specifically the ForEach(...)
My goal is to replace the " " with an empty string; is there a better way to do this?
var lastNames = "SMITH, JOHNSON, WILLIAMS, JONES, BROWN";
var listLastNames = lastNames.Split(',');
var list = listLastNames.ToList(); //so I can use .ForEach
list.ForEach(i=>i.Replace(" ",String.Empty));
list.Dump(); //show it on output
As others have pointed out, strings are immutable. Calling Replace simply returns a new string; it does not mutate the existing string in place. Here are three ways to do what you want:
Do the transformation on the sequence, and convert it to a list at the end:
string s = "SMITH, JOHNSON, WILLIAMS, JONES, BROWN";
List<string> lastNames = s.Split(',').Select(x=>x.Trim()).ToList();
Or, the same thing in query syntax:
string s = "SMITH, JOHNSON, WILLIAMS, JONES, BROWN";
var query = from lastName in s.Split(',')
select lastName.Trim();
List<string> lastNames = query.ToList();
Or, make an array and mutate the array in place:
string s = "SMITH, JOHNSON, WILLIAMS, JONES, BROWN";
string[] lastNames = s.Split(',');
for (int i = 0; i < lastNames.Length; ++i)
lastNames[i] = lastNames[i].Trim();
Replace returns a new value, but doesn't affect the original String that you call it on. To do what you need, you'd have to build a new collection with the results from Replace - you can do this easily with Select:
var replaced = list.Select(i=>i.Replace(" ",String.Empty));
Another benefit, you won't need to cast to a List<T> to do this.
And as others have pointed out, you can use Trim() as a cleaner solution than Replace():
var collection = lastNames.Split(',').Select(i => i.Trim());

Merge two (or more) lists into one, in C# .NET

Is it possible to convert two or more lists into one single list, in .NET using C#?
For example,
public static List<Product> GetAllProducts(int categoryId){ .... }
.
.
.
var productCollection1 = GetAllProducts(CategoryId1);
var productCollection2 = GetAllProducts(CategoryId2);
var productCollection3 = GetAllProducts(CategoryId3);
You can use the LINQ Concat and ToList methods:
var allProducts = productCollection1.Concat(productCollection2)
.Concat(productCollection3)
.ToList();
Note that there are more efficient ways to do this - the above will basically loop through all the entries, creating a dynamically sized buffer. As you can predict the size to start with, you don't need this dynamic sizing... so you could use:
var allProducts = new List<Product>(productCollection1.Count +
productCollection2.Count +
productCollection3.Count);
allProducts.AddRange(productCollection1);
allProducts.AddRange(productCollection2);
allProducts.AddRange(productCollection3);
(AddRange is special-cased for ICollection<T> for efficiency.)
I wouldn't take this approach unless you really have to though.
Assuming you want a list containing all of the products for the specified category-Ids, you can treat your query as a projection followed by a flattening operation. There's a LINQ operator that does that: SelectMany.
// implicitly List<Product>
var products = new[] { CategoryId1, CategoryId2, CategoryId3 }
.SelectMany(id => GetAllProducts(id))
.ToList();
In C# 4, you can shorten the SelectMany to: .SelectMany(GetAllProducts)
If you already have lists representing the products for each Id, then what you need is a concatenation, as others point out.
you can combine them using LINQ:
list = list1.Concat(list2).Concat(list3).ToList();
the more traditional approach of using List.AddRange() might be more efficient though.
List.AddRange will change (mutate) an existing list by adding additional elements:
list1.AddRange(list2); // list1 now also has list2's items appended to it.
Alternatively, in modern immutable style, you can project out a new list without changing the existing lists:
Concat, which presents an unordered sequence of list1's items, followed by list2's items:
var concatenated = list1.Concat(list2).ToList();
Not quite the same, Union projects a distinct sequence of items:
var distinct = list1.Union(list2).ToList();
Note that for the 'value type distinct' behaviour of Union to work on reference types, that you will need to define equality comparisons for your classes (or alternatively use the built in comparators of record types).
You could use the Concat extension method:
var result = productCollection1
.Concat(productCollection2)
.Concat(productCollection3)
.ToList();
I know this is an old question I thought I might just add my 2 cents.
If you have a List<Something>[] you can join them using Aggregate
public List<TType> Concat<TType>(params List<TType>[] lists)
{
var result = lists.Aggregate(new List<TType>(), (x, y) => x.Concat(y).ToList());
return result;
}
Hope this helps.
list4 = list1.Concat(list2).Concat(list3).ToList();
// I would make it a little bit more simple
var products = new List<List<product>> {item1, item2, item3 }.SelectMany(id => id).ToList();
This way it is a multi dimensional List and the .SelectMany() will flatten it into a IEnumerable of product then I use the .ToList() method after.
I've already commented it but I still think is a valid option, just test if in your environment is better one solution or the other. In my particular case, using source.ForEach(p => dest.Add(p)) performs better than the classic AddRange but I've not investigated why at the low level.
You can see an example code here: https://gist.github.com/mcliment/4690433
So the option would be:
var allProducts = new List<Product>(productCollection1.Count +
productCollection2.Count +
productCollection3.Count);
productCollection1.ForEach(p => allProducts.Add(p));
productCollection2.ForEach(p => allProducts.Add(p));
productCollection3.ForEach(p => allProducts.Add(p));
Test it to see if it works for you.
Disclaimer: I'm not advocating for this solution, I find Concat the most clear one. I just stated -in my discussion with Jon- that in my machine this case performs better than AddRange, but he says, with far more knowledge than I, that this does not make sense. There's the gist if you want to compare.
To merge or Combine to Lists into a One list.
There is one thing that must be true: the type of both list will be
equal.
For Example: if we have list of string so we can add add another list to the
existing list which have list of type string otherwise we can't.
Example:
class Program
{
static void Main(string[] args)
{
List<string> CustomerList_One = new List<string>
{
"James",
"Scott",
"Mark",
"John",
"Sara",
"Mary",
"William",
"Broad",
"Ben",
"Rich",
"Hack",
"Bob"
};
List<string> CustomerList_Two = new List<string>
{
"Perter",
"Parker",
"Bond",
"been",
"Bilbo",
"Cooper"
};
// Adding all contents of CustomerList_Two to CustomerList_One.
CustomerList_One.AddRange(CustomerList_Two);
// Creating another Listlist and assigning all Contents of CustomerList_One.
List<string> AllCustomers = new List<string>();
foreach (var item in CustomerList_One)
{
AllCustomers.Add(item);
}
// Removing CustomerList_One & CustomerList_Two.
CustomerList_One = null;
CustomerList_Two = null;
// CustomerList_One & CustomerList_Two -- (Garbage Collected)
GC.Collect();
Console.WriteLine("Total No. of Customers : " + AllCustomers.Count());
Console.WriteLine("-------------------------------------------------");
foreach (var customer in AllCustomers)
{
Console.WriteLine("Customer : " + customer);
}
Console.WriteLine("-------------------------------------------------");
}
}
In the special case: "All elements of List1 goes to a new List2": (e.g. a string list)
List<string> list2 = new List<string>(list1);
In this case, list2 is generated with all elements from list1.
You need to use Concat operation
When you got few list but you don't know how many exactly, use this:
listsOfProducts contains few lists filled with objects.
List<Product> productListMerged = new List<Product>();
listsOfProducts.ForEach(q => q.ForEach(e => productListMerged.Add(e)));
If you have an empty list and you want to merge it with a filled list, do not use Concat, use AddRange instead.
List<MyT> finalList = new ();
List<MyT> list = new List<MyT>() { a = 1, b = 2, c = 3 };
finalList.AddRange(list);

Categories