Thought that I should share this with you.
Many times we will end up using List as our collection holder.
And that many time we wonder how to find if a value exists in List.
There are many ways you can do it. I’ll show you a couple which required less coding.
I must admit that I am not a c# pro but learning it on the go for my home made tools which requires me to get my hands dirty with c#.
Alright now lets get to the point
First we create a List.
Below is a basic List object
lt1.Add("abc"); lt1.Add("def"); lt1.Add("hello"); lt1.Add("jas"); lt1.Add("how"); lt1.Add("are"); lt1.Add("you");
Now lets check if value “hello” exist in lt1or not
bool exist = lt1.Any(item => item == "hello");
I am using LINQ expression above. Above line should leave value “True” in variable “exist“
Another way is using function Find on List
string txt = lt1.Find(item => item == "hello");
Above line will leave txt with value hello
There are other handy methods to find value in List but I found above the most useful. One liners and does the Job nicely
I hope this helps








Recent Comments