I have an array of words
string[] words = { "believe", "relief", "receipt", "field" };
How to find out the words with the substring "ei" using linq
IEnumerable<string> iswordlist = from word in words where words.All(a => a.Contains("ei"))
select word;
foreach (var i in iswordlist)
{
txtlinq.Text = txtlinq.Text + i.ToString();
}
I tried the above but got no result. Can anyone help me on this?
Try like this
var newWord = words.Where(o => o.Contains("ei"));
I'm not very good with free form, but with dot syntax this will work:
var ieWords = words.Where(a => a.Contains("ei"));
Your current code won't even compile because you are using Enumarbale.All method which returns either true or false (when given condition matches) and not an IEnumerable on which you can query. You simple need a Where clause.
Try this:-
var result = words.Where(x => x.Contains("ei"));
Or if you prefer query syntax:-
IEnumerable<string> result = from word in words
where word.Contains("ei")
select word;
Working Fiddle.
Related
I have a string[] containing ie:
abcd
Saving C:\\xx
aacd
Saving C:\\yy
aecd
Saving C:\\zz
and so on
Is there a way in linq that searches all the lines and returns only C:\\xx, C:\\yy and C:\\zz to a list/array.
This is what i tried so far:
string[] line = result.Split(new[] { '\r', '\n' });
string searchTerms = "Saving ";
var results = (from comp in line.ToString()
where searchTerms.All(s => comp.Contains(s))
select comp).ToList();
You don't need All method, you can just use:
where comp.StartsWith(searchTerms)
And instead of splitting on \r and \n it's better to use:
string[] line = result.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
And you should also remove ToString from line.ToString()
var results = (from comp in line
where comp.StartsWith(searchTerms)
select comp).ToList();
I don't, really understand your kind of input data, but if it is like this
string[] str = new string[] { "abcd", "Saving C:\\xx", "aacd", "Saving C:\\yy", "aecd", "Saving C:\\zz"};
then you can do the following:
List<string> result = str.Where(s => s.StartsWith("Saving C:\\")).ToList();
If string ends path:
string result = #"abcd
Saving C:\xx
aacd
Saving C:\yy
aecd
Saving C:\zz
and so on";
string[] line = result.Split(new[] { '\r', '\n' });
string searchTerms = "Saving ";
var lst = line.Where(x => x.StartsWith(searchTerms))
.Select(y => y.Substring(searchTerms.Length));
foreach (var x in lst)
Console.WriteLine(x);
http://ideone.com/OXDMO5
This should give you a simple IEnumerable containing each path.
NOTE: This will only return the part of each line after whatever is in searchTerms.
var result =
line.Where(l => l.StartsWith(searchTerms))
.Select(c => new string(c.Skip(searchTerms.Count()).ToArray()));
If all you want to do is get the paths from a string you can use regular expressions for this:
var result = "abcd\r\nSaving C:\\xx\r\naacd\r\nSaving C:\\yy\r\naecd\r\nSaving C:\\zz\r\n";
var regex = new Regex(#"(?<=Saving )(.*)");
var matches = regex.Matches(result).Cast<Match>().Select(m => m).ToList();
Edit
Another more generic form of regular expression you can use to get all paths (not just the ones with Savingbefore them):
var matches = Regex.Matches(result, ".:\\\\(.*?)(?=\s.*)", RegexOptions.Multiline).Cast<Match>().Select(m => m).ToList();
This also removes any whitespace after the paths.
Edit 2
To also get paths within quotes (paths with spaces) you can use this regular expression:
var regex = new Regex("((\"\\w|\\w):\\\\)((.*\")|(.*?)(?=\\s.*))");
I am reading a file that contains rows like
pathName; additionalString; maybeSomeNumbers
I read it using
var lines = File.ReadAllLines(fileListFile);
var fileListEntries = from line in lines
where !line.StartsWith("#")
select line.Split(';').ToArray();
This works well so far. However I would like to change the drive letter in the pathName. I could convert fileListEntries to an array and loop across elements [i][0], but is there a way that I could do this operation on the collection directly?
Use the LINQ extension method syntax in order to be able to use code blocks { ... } in the lambda expressions. If you do so, you have to include an explicit return-statement.
var fileListEntries = lines
.Where(l => !l.StartsWith("#"))
.Select(l => {
string[] columns = l.Split(';');
if (Path.IsPathRooted(column[0])) {
string root = Path.GetPathRoot(columns[0]);
columns[0] = Path.Combine(#"X:\", columns[0].Substring(root.Length));
}
return columns;
})
.ToArray();
I think you can do it inline with the LINQ.
File.ReadAllLines() returns a string array, so you should be able to perform Replace() on the line from the collection.
var replace = "The string to replace the drive letter";
var lines = File.ReadAllLines(fileListFile);
var fileListEntries = from line in lines
where !line.StartsWith("#")
select (line.Replace(line[0], replace).Split(';')).ToArray();
You could just call a method in your select that modifies the text in the manner that you would like.
static void Main(string[] args)
{
var fileListEntries = from line in lines
where !(line.StartsWith("#"))
select ( ModifyString(line));
}
private static string[] ModifyString(string line)
{
string[] elements = line.Split(';');
elements[0] = "modifiedString";
return elements;
}
lines.Where(l => !l.StartsWith("#").
Select(l => string.Concat(driveLetter, l.Substring(1))).
Select(l => l.Split(';');
My values come from ComboBox:
2|722|742|762|77
I delete unnecessary characters as follows:
foreach (var item in checkListBox)
{
string[] list = item.Split(
new string[] { "2|" },
StringSplitOptions.RemoveEmptyEntries);
}
My list values result:
"72"
"74"
"76"
"77"
My question is:
how can I get all of the above values in 1 row (next to each other) separated by comma like this:
72,74,76,77
?
It sounds like you just want string.Join:
string commaSeparated = string.Join(",", list);
(Note that this is not part of LINQ - it's not the same kind of "join" as for joining multiple sequences. It's joining several strings together with a separator.)
While you can do this in a single statement as per the currently accepted answer, I'd personally consider leaving your existing statement and having this as a separate step. It makes the code easier to both read and debug.
String.Join(",",list);
Though: a) This is not Linq. b) As is mentioned in another answer here - It would be simpler in this case to use Replace.
Using Linq:
list.Select(s => s + ",").Aggregate((s, q) => s + q).TrimEnd(',');
How about
var result = string.Join(",", item.Split(new string[] { "2|" }, StringSplitOptions.RemoveEmptyEntries));
Just use Replace directly:
string input = "2|722|742|762|77";
var result = input.Replace("2|",",").Trim(',');
As noted in the other answers, string.Join is what should be used here. If you'd however insist on LINQ:
var result = list
.Skip(1)
.Aggregate(
list.FirstOrDefault() ?? string.Empty,
(total, current) => total + "," + current);
I have a list of strings (file names actually) and I'd like to keep only those that match a filter expression like: \*_Test.txt.
What would be the best to achieve this?
Here is the answer that I came up with:
List<string> files = new List<string>();
files.Add("MyFile1.csv");
files.Add("MyFile1_Test.txt");
files.Add("MyFile2.csv");
files.Add("MyFile2_Test.txt");
files.Add("MyFile3.csv");
files.Add("MyFile3_Test.txt");
files.Add("MyFile_Testtxttxt.txt");
// Define a filter
string filter = "*_Test.txt";
// Make the filter regex safe
foreach (char x in #"\+?|{[()^$.#")
filter = filter.Replace(x.ToString(), #"\" + x.ToString());
filter = string.Format("^{0}$",filter.Replace("*", ".*"));
// Old School
List<string> resultList1 = files.FindAll(delegate(string s) { return Regex.IsMatch(s, filter, RegexOptions.IgnoreCase); });
// Version using LINQ
List<string> resultList2 = files.Where(x => Regex.IsMatch(x, filter, RegexOptions.IgnoreCase) == true ).ToList();
You probably want to use a regular expression for this if your patterns are going to be complex....
you could either use a proper regular expression as your filter (e.g for your specific example it would be new Regex(#"^.*_Test\.txt$") or you could apply a conversion algorithm.
Either way you could then just use linq to apply the regex.
for example
var myRegex=new Regex(#"^.*_Test\.txt$");
List<string> resultList=files.Where(myRegex.IsMatch).ToList();
Some people may think the above answer is incorrect, but you can use a method group instead of a lambda. If you wish the full lamda you would use:
var myRegex=new Regex(#"^.*_Test\.txt$");
List<string> resultList=files.Where(f => myRegex.IsMatch(f)).ToList();
or non Linq
List<string> resultList=files.FindAll(delegate(string s) { return myRegex.IsMatch(s);});
if you were converting the filter a simple conversion would be
var myFilter="*_Test.txt";
var myRegex=new Regex("^" + myFilter.Replace("*",".*") +"$");
You could then also have filters like "*Test*.txt" with this method.
However, if you went down this conversion route you would need to make sure you escaped out all the special regular expression chars e.g. "." becomes #".", "(" becomes #"(" etc.......
Edit -- The example replace is TOO simple because it doesn't convert the . so it would find "fish_Textxtxt" so escape atleast the .
so
string myFilter="*_Test.txt";
foreach(char x in #"\+?|{[()^$.#") {
myFilter = myFilter.Replace(x.ToString(),#"\"+x.ToString());
}
Regex myRegex=new Regex(string.Format("^{0}$",myFilter.Replace("*",".*")));
Have you tried LINQ:
List<string> resultList = files.Where(x => x.EndsWith("_Test.txt")).ToList();
or if you are running this on some old/legacy .NET version (< 3.5):
List<string> resultList = files.FindAll(delegate(string s) {
return s.EndsWith("_Test.txt");
});
This worked for me and is fairly simple:
List<string> keys = new List<string>();
//populate your list
var myregex = new Regex("^.+$");
List<string> matchlist = keys.Where(i=>myregex.IsMatch(i)).ToList();
I have this c# code that builds a string of comma seperated matches for a service:
for (m = r.Match(site); m.Success; m = m.NextMatch())
{
found = found + "," + m.Value.Replace(",", "");
}
return found;
Output looks like: aaa,bbb,ccc,aaa,111,111,ccc
Now that code is on .NET 4.0 How can I use C# LINQ to remove duplicates?
Also, Any way to remove duplicates without changing order?
I found this sample code in another post, but not sure exactly how to apply it:
int[] s = { 1, 2, 3, 3, 4};
int[] q = s.Distinct().ToArray();
Thanks.
string[] s = found.Split(',').Distinct().ToArray()
Rewrite the code that builds the result to output it directly.
ie. rewrite this:
for (m = r.Match(site); m.Success; m = m.NextMatch())
{
found = found + "," + m.Value.Replace(",", "");
}
return found;
To this:
return (from Match m in r.Matches(site)
select m.Value.Replace(",", "")).Distinct().ToArray();
This will return an array. If you still want it back as a string:
return string.Join(", ", (from Match m in r.Matches(site)
select m.Value.Replace(",", "")).Distinct().ToArray());
You may or may not be able to remove the last .ToArray() from the last code there depending on the .NET runtime version. .NET 4.0 string.Join(...) can take an IEnumerable<string>, whereas previous versions requires an array.
This will return a string of comma seperated values without duplicates:
var result = string.Join(",",
r.Matches(site)
.Cast<Match>()
.Select(m => m.Value.Replace(",", string.Empty))
.Distinct()
);
this could be one possible solution:
var data = new List<string>();
for (m = r.Match(site); m.Success; m = m.NextMatch())
data.Add(m.Value.Replace(",", ""));
return String.Join(",", data.Distinct().ToArray());
You can achieve this in a single LINQ query
string strSentence = "aaa,bbb,ccc,aaa,111,111,ccc";
List<string> results = (from w in strSentence.Split(',') select w).Distinct().ToList();