I've a notepad file that has the following format:
at-2017#yahoo.com
at-2017#yahoo.com
at-2018#yahoo.com
at-2018#yahoo.com
I require the following distinct output:
at-2017#yahoo.com
at-2018#yahoo.com
Tried the following code but it doesn't get distinct values:
List<string> lst = new List<string>();
foreach (string line in File.ReadLines(values))
{
line.Distinct().ToString();
lst.Add(line);
}
I know, this may seem stupid and guessing, missed something here.
First you should read all the lines and then get the distinct lines:
var allLines = File.ReadLines(values);
var distinctLines = allLines.Distinct();
foreach(var distinctLine in distinctLines)
{
Console.WriteLine(distinctLine);
}
Distinct() operates on a collection of elements, so you don't need to use it inside the loop.
Try following:
var lst = File.ReadLines(values).Distinct();
foreach (string line in lst)
{
Console.WriteLine(line) ;
}
Related
.net fiddle: https://dotnetfiddle.net/27wJLc
I have two lists of strings, the 'all' list and a list to compare, I then want to return the strings from the 'All' list that did not match:
public static void Main()
{
List<string> ratiodGids = new List<string>();
List<string> allGids = new List<string>();
List<string> notRatiodGids = new List<string>();
allGids.Add("tom");
allGids.Add("bob");
allGids.Add("bill");
allGids.Add("tim");
allGids.Add("sam");
ratiodGids.Add("tom");
ratiodGids.Add("tim");
ratiodGids.Add("sam");
foreach(var g in ratiodGids)
{
if(!allGids.Contains(g))
{
notRatiodGids.Add(g);
}
}
Console.WriteLine(notRatiodGids.Count);
foreach(var i in notRatiodGids)
Console.WriteLine(i);
}
I'm pretty sure I can re-type everything back to IEnumerable then use Intersect or Except, but that will take some work. So I wanted to see if it was possible with Lists before doing that.
With the code I have, the list returns nothing. If I remove the '!' I get back tom, tim, sam. So it works one way, but not the other.
UPDATE: The result I want is just the strings from ratiodGids that don't exist in allGids. Order does not matter. Also, I have tried using Exists(), Except(), Any(), Where() and they both give me the "List does not contain a method called blah ...".
Well, you can use Except() on a List as well, because it implements IList which derives from IEnumerable.
Update
As pointed out in the comments, the Linq-methods are extension methods on IEnumerable. To use them, you have to add their namespace by adding: using System.Linq;
You can compare two lists and return a new list with only the differences like so:
List<string> difference = list1.Except(list2).ToList();
You can do it like:
var results = allGids.Where( a=> !ratiodGids.Contains(a))
if you want to get uniqe items from both lists then:
var results = allGids.Where( a=> !ratiodGids.Contains(a))
.Union(ratiodGids.Where( a=> !allGids.Contains(a)));
change this
foreach(var g in ratiodGids)
{
if(!allGids.Contains(g))
{
notRatiodGids.Add(g);
}
}
to this
foreach(var g in allGids)
{
if(!ratioGids.Contains(g))
{
notRatiodGids.Add(g);
}
}
To be clear, if listA = { A,B,C } and listB = { B,C,D } do you want your result to be { A,D }? If so, here's one way:
var union = listA.Union(listB);
var intersection = listA.Intersect(listB);
var outside = union.Except(intersection);
I have a list of strings in a List container class that look like the following:
MainMenuItem|MenuItem|subItemX
..
..
..
..
MainMenuItem|MenuItem|subItem99
What I am trying to do is transform the string, using LINQ, so that the first item for each of the tokenised string is removed.
This is the code I already have:
protected static List<string> _menuItems = GetMenuItemsFromXMLFile();
_menuItems.Where(x => x.Contains(menuItemToSearch)).ToList();
First line of code is returning an entire XML file with all the menu items that exist within an application in a tokenised form;
The second line is saying 'get me all menu items that belong to menuItemToSearch'.
menuItemToSearch is contained in the delimited string that is returned. How do I remove it using linq?
EXAMPLE
Before transform: MainMenuItem|MenuItem|subItem99
After transform : MenuItem|subItem99
Hope the example illustrates my intentions
Thanks
You can take a substring from the first position of the pipe symbol '|' to remove the first item from a string, like this:
var str = "MainMenuItem|MenuItem|subItemX";
var dropFirst = str.Substring(str.IndexOf('|')+1);
Demo.
Apply this to all strings from the list in a LINQ Select to produce the desired result:
var res = _menuItems
.Where(x => x.Contains(menuItemToSearch))
.Select(str => str.Substring(str.IndexOf('|')+1))
.ToList();
Maybe sth like this can help you.
var regex = new Regex("[^\\|]+\\|(.+)");
var list = new List<string>(new string[] { "MainMenuItem|MenuItem|subItem99", "MainMenuItem|MenuItem|subItem99" });
var result = list.Where(p => regex.IsMatch(p)).Select(p => regex.Match(p).Groups[1]).ToList();
This should work correctly.
I have a list o that has strings:
"Hist 2368#19:00:00#20:30:00#Large Conference Room",
"Hist 2368#09:00:00#10:30:00#Large Conference Room",
I want to add those strings to this:
var lines = new[]
{
"Meeting#19:00:00#20:30:00#Conference",
};
How would I use the data from the list o and insert it into lines?
Array are be nature, fixed-length. You need to create a new array, and assign it to lines.
lines = lines.Concat(o).ToArray();
Alternately,
lines = o.AddRange(lines).ToArray();
UPDATE: Fixed dumb mistake.
Since Lines is already an array, you'll need to merge the values into your list first:
foreach (var item in lines)
o.Add(item);
Then change o to an Array:
o.ToArray(); ///returns String[] with all three values.
You can also use .concat() as others have pointed out, which will internally do the same.
An additional thing to consider is the lines variable has an Array(T) type which is a fixed size, so you must either allocate enough space to hold all the data or copy the data into a new construct.
If you allocated enough space for lines to hold all the data then it looks something like this:
var o = new List<string>
{
"Hist 2368#19:00:00#20:30:00#Large Conference Room",
"Hist 2368#09:00:00#10:30:00#Large Conference Room",
};
var lines = new string[3] { "Meeting#19:00:00#20:30:00#Conference", null, null };
// Copy the data from o to the end of lines
o.CopyTo(lines, 1); // Start a 1 to not overwrite the existing data
See also:
CopyTo(T[] array)
Otherwise if you have two different data sources that you want to pool into a new construct then I recommend using the Concat method. This will combine the IEnumerable(T) types which you can use ToArray or ToList to give the data the right container.
var o = new List<string>
{
"Hist 2368#19:00:00#20:30:00#Large Conference Room",
"Hist 2368#09:00:00#10:30:00#Large Conference Room",
};
var lines = new[]
{
"Meeting#19:00:00#20:30:00#Conference",
}.Concat(o).ToArray();
Be sure you know which container you want to use.
You can concatenate the values using Enumerable.Concat :
lines = O.Concat(lines).ToArray();
Simply this
lines = lines.Concat(o).ToArray();
Try this:
List<string> myList = new List<string>()
{
"My First String in List",
"My Second String in List"
};
string[] myArray = new string[] { "My Array First String" };
List<string> myArrayList = new List<string>();
foreach (var item in myArray)
{
myArrayList.Add(item);
}
foreach (var item in myList)
{
myArrayList.Add(item);
}
foreach (var item in myArrayList)
{
Console.WriteLine(item);
}
Console.Read();
I've a collection list.
List<string> mycollections = new List<string>(new string[]
{
"MyImages/Temp/bus.jpg",
"MyImages/Temp/car.jpg",
"MyImages/Temp/truck.jpg",
"MyImages/Temp/plane.jpg",
"MyImages/Temp/ship.jpg",
});
I required only files in a List such asbus.jpg, car.jpg...... Here i do not need "MyImages/Temp/" portion of the string in the same list.
I tried with Substring and Split with Linq queries but couldn't get the expected result.
Use Path.GetFileName instead of substring like:
var fileNames = mycollections.Select(r => Path.GetFileName(r)).ToList();
For output:
var fileNames = mycollections.Select(r => Path.GetFileName(r));
foreach (var item in fileNames)
{
Console.WriteLine(item);
}
Output:
bus.jpg
car.jpg
truck.jpg
plane.jpg
ship.jpg
How about this:
mycollections.Select(s => s.Split('/').Last());
That will split each string by slashes and return the last item.
i need to find out a string from collection of strings using its sub string. This sub string
must be in starting.
collection.FirstOrDefault(s => s.StartsWith(whatever))
you can do something like this,
List<string> collection = new List<string>();
collection.Add("example sample");
collection.Add("sample");
collection.Add("example");
var varSubstring = collection.Where(x => x.IndexOf("sample")==0).ToList();
foreach (var vartemp in varSubstring)
{
}