I have a list of objects, and i need to filter that objects that one of his properties DONT start with some letter, i see that i can do it:
onelist=mylist.where(x=>x.Cod.startwith("A"));
but what i need is just the contrary, something like:
onelist=mylist.where(x=>x.Cod.NOTstartwith("A"));
I dont know if it´s posible, if that it´s not posible i seem to remember that there was a way to in one lamda expression negate other, is correct??
Thanks
Just use standard boolean operator:
onelist = mylist.Where(x => !x.Cod.StartsWith("A"));
Related
I have a C# list full of strings.
I would like to exclude those items that match a specific pattern like those strings that start with "DST*"
Any string that doesn't start with DST, am happy to take, but anything that starts with DST I would like to remove from this list.
I see linq has Contains(). But how do I do the inverse of Contains()?
You can use the ! operator to inverse any bool:
var filtered = listOfStrings.Where(x => !x.StartsWith("DST"));
anything that starts with DST I would like to remove from this list.
You don't need LINQ you could also use List<T>.RemoveAll:
listOfStrings.RemoveAll(s => s.StartsWith("DST"));
But of course this modifies the original list and does not create a new. If that's desired use the approach from Somebody.
i have a question if i can define a String.contain("-") to search for specific character "-" followed by one or more integers.
so it will cover something such as :-
search -12
t-123est
but will not cover
search-t12
t-est123
You best option might not be to use String.Contains, You might be best served using Regex.IsMatch. With that you can define a regular expression that will exactly match your needs. you can use sites like https://www.regex101.com/ to test your expression to make sure it covers your cases. In your case, you can use
Regex.IsMatch(myString, #"-\d+")
This would be enough:
Regex.IsMatch("search -12", #"-\d")
What is the best practice way to use two clauses in LINQ Contains method..
Title is string
This is my If statement :
if (oWeb.ParentWeb.Title.Contains("Ricky") || oWeb.ParentWeb.Title.Contains("John"))
I need solution like this :
if (oWeb.ParentWeb.Title.Contains("Ricky", "John"))
Since Title is string this actually has nothing to do with LINQ as the used Contains method is an instance method of string.
Assuming you have more strings to check, you can do something like that:
var strings = new[] {"Ricky", "John"};
if (strings.Any(oWeb.ParentWeb.Title.Contains))
// do something
But roryap's answer using a regex seems preferable (as long as the number of strings to check is not too big).
I don't think LINQ is the best option for that. Why not use regular expressions?
Regex.IsMatch(oWeb.ParentWeb.Title, "ricky|john", RegexOptions.IgnoreCase);
Contains takes only one parameter, so you cannot do it this way.
You can make an array of items, and check containment with it:
var titles = new[] {"Ricky", "John"};
if (titles.Any(t => oWeb.ParentWeb.Title.Contains(t))) {
...
}
I use MVC3, C#, .NET4
I have a pattern ie
"irc:tl:tr,sp"
I am only interested in
"irc:tl:sp" bit.
However a straight
string.Contains("irc:tl:sp")
will return false due to presenvr of "tr", so I need something like a wildcard ie
string.Contains("irc:tl:*sp")
Is there a way of achieving this wildcard approach or am I needing Regex?.
Thanks.
You could use Regex.IsMatch:
Regex.IsMatch(str, #"irc:tl:.*,sp")
You could use regular expressions, but if you just want to use a simple wildcard pattern, my NString library has a MatchesWildcard extension method to do just that:
bool result = "irc:tl:tr,sp".MatchesWildcard("irc:tl:*sp"); // true
I know the want for a "multi replace" in C# is no new concept, there are tons of solutions out there. However, I haven't came across any elegant solution that jives well with Linq to Sql. My proposal is to create an extension method (called, you guessed it, MultiReplace) that returns a lambda expression which chains multiple calls of Replace together. So in effect you would have the following:
x => x.SomeMember.MultiReplace("ABC", "-")
// Which would return a compiled expression equivalent to:
x => x.SomeMember.Replace("A", "-").Replace("B", "-").Replace("C", "-")
I'd like your thoughts/input/suggestions on this. Even if it turns out to be a bad idea, it still seems like a wicked opportunity to dive into expression trees. Your feedback is most appreciated.
-Eric
I'm not completely sure why you would want to do what you're describing. However if your motivation is to make more readable linq statements, by condensing some filtering logic, I suggest to look into the Specification Pattern.
If you only want to transform the result however, I would suggest to just do it in code, as there would only be a marginal benefit transforming on the server.
Some more examples on the Specification Pattern and Linq-to-SQL
I'm not sure what MultiReplace should be doing, or why you want to mix it with Linq to Sql. (Anything truly working with Linq to Sql would be translatable into SQL, which would be quite a lot of work, I think.)
The best solution I can think of is Regular Expressions. Why not use them? Linq to Sql may even translate them for you already, since MS SQL supports regular expressions.
x => Regex.Replace(x, "A|B|C", "-")
To me, this seems like a REALLY bad idea, it could be just because of your example, but the syntax you have listed to me is very confusing.
Reading this I would expect that
x => x.SomeMember.MultiReplace("ABC", "-")
If using the following text
ABC This Is A Test ABC Application
You would get something like
--- This Is A Test --- Application
But you are actually saying that it would be
--- This Is - Test --- -pplication
Which I see as problematic....
I'd also mention here that I don't really see a n urgent need for something like this. I guess if there was a need to do multiple replacemnts I'd do one of the following.
Chain them myself "myInput".Replace("m", "-").Replace("t", "-")
Create a function/method that accepts an ARRAY or List of strings for the matches, then a replacement character. Keeping it easy to understand.
Maybe there's no method that will do this, but you could simply nest the calls. Or make a static method that takes in a string and an array containing all its replacements. Another problem is that you need to actually specify a pair (the original substring, and its replacement).
I've never needed/wanted a feature like this.
The best way to do this would be:
static string MultiReplace(string this CSV, string Orig, string Replacement)
{
string final = "";
foreach (string s in CSV.Split(','))
{
final += s.Replace(Orig, Replacement);
}
return final;
}
Then you could call it with no ambiguity:
x => x.SomeMember.MultiReplace("A,B,C", "-")
If you expect that these strings will be longer than 3-4 values in the CSV, you might want to drop the concatenation in favor of a StringBuilder.