Im trying to replace items in a list with the RemoveAll function, but didnt realize that it completely removes the index of the list. Is there a function that can replace objects instead of remove them? I am comparing strings.
Items.RemoveAll(x => commonItems.Any(y => y.ItemName == x.ItemName));
Or maybe once its removed I replace it with another empty index at that same spot?
You could replace RemoveAll with Where to get the records you want to modify first:
var matches = Items.Where(x => commonItems.Any(y => y.ItemName == x.ItemName));
Then just iterate through the results and replace the value:
foreach (var match in matches)
match.ItemName = "N/A";
Related
I would like to GroupBy element using a field (CompteNum) that has space in it. So I would like to trim the field using this code
var excelLinesGrouped = excelLines
.GroupBy(l => l.CompteNum.Trim(),
(entry, elements) =>
new
{
Entry = entry,
Writings = elements
});
It gives null results probably meaning that trim is not supported in GroupBy. Is there any alternative for this?
Thanks
I am saving multiple tuples that contain strings in a list:
List<Tuple<string, string, string>> ExcelRawImport
What i want to do now, is find the tuple in the list, where the Tuple.Item1 has an specific value.
how should i do this ?
With Linq I would say
var specificValue = "mySpecificValue";
var foundTuple = ExcelRawImport.FirstOrDefault(eri => eri.Item1 == specificValue);
This returns the first Item from your list that matches the specific value. If no matching value is found, foundTuple is null.
If you need all items that match you can use the Where() statement instead.
var foundTuples = ExcelRawImport.Where(eri => eri.Item1 == specificValue).ToList();
I am trying to convert the following code to LINQ
foreach (var item in pageHistorycol)
{
if (item.PageTitle.Contains(text) || item.PageURI.Contains(text))
{
tempHistory.Insert(0, item);
}
}
The following returns all items instead of the matches
var matches = pageHistorycol.Where(item =>
(item.PageTitle.Contains(text) || item.PageURI.Contains(text)));
What am I missing?
The two statements you displayed should return the same items. If the second returns all items, the foreach loop should add all of the items to tempHistory as well.
The main difference will be your foreach loop will return the items in the opposite order as your LINQ query. If the order of matches is important, you can get the items in the same order using:
var matches = pageHistorycol
.Where(item => item.PageTitle.Contains(text) || item.PageURI.Contains(text))
.Reverse();
I have a list like this:
item.Add("a");
item.Add("as");
item.Add("b");
item.Add("fgs");
item.Add("adsd");
How can I find all items that start with (for example) "a"?
This "a" is not some hardcoded string, so I will need a function that do this for each string.
I try with FindAll, but I did not figured out how it works.
Br, Wolfy
If by "start with" you mean the first char, then:
item.FindAll(i => i[0] == 'a');
if you mean a string (may be other than 1 char) then:
item.FindAll(i => i.StartsWith("a"));
If you want a different comparison, such as case-insensitive, locale-based, etc. then do the relevant IndexOf such as:
item.FindAll(i => i.IndexOf("a", StringComparison.CurrentCultureIgnoreCase) == 0);
All of the above can be easily adapted to be use a relevant char or string variable or parameter.
If you don't need the extra properties and methods provided by a list, then it will be more efficient to use Where than FindAll as FindAll creates a new list, and does so in all at once, while Where will enumerate the matching results as it is iterated through.
Or with LINQ
from i in items where i.StartsWith("a") select i;
for NET2.0 you may use this method:
'pattern' is an argument to look for (f.e. "a")
private List<string> FindAll(List<string> list, string pattern)
{ // returns found results
return list.FindAll(delegate(string item)
{
return item.StartsWith(pattern);
});
}
I thought you have another list that contains the startswith criteria strings. Lets call your items "words" and the other list "keywords". So the below query will return what you want.
List<string> words = new List<string>();
words.Add("a");
words.Add("as");
words.Add("b");
words.Add("fgs");
words.Add("adsd");
List<string> keywords = new List<string>();
keywords.Add("a");
keywords.Add("b");
var result = words.FindAll(o =>
keywords.Any(a => o.StartsWith(a))
);
This result has the words that starts with any of the keyword from keywords.
List<string> item = new List<string>();
item.Add("a");
item.Add("as");
item.Add("b");
item.Add("fgs");
item.Add("adsd");
var res1 = item.FindAll(i => i.StartsWith("a"));
var res2 = item.Where(i => i.StartsWith("a"));
Try this
item.FindAll(i => i.Contains("a"));
This will return a List containting only the filtered strings.
var list=alist.Contains("somestring")
this matches whole string, how to see if any word in list has a substring matching "somestring"?
You can use the Enumerable.Any method:
bool contained = alist.Any( l => l.Contains("somestring") );
This is checking each element using String.Contains, which checks substrings. You previously were using ICollection<string>.Contains(), which checks for a specific element of the list.
var hasPartialMatch = alist.Split(' ').ToList()
.Any(x => x.Contains("somestring"));