The text of this article is not in this database — only its details are. Read it on the old site: Using a Lambda Expression Over a List in C#
17 Comments
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment.
The text of this article is not in this database — only its details are. Read it on the old site: Using a Lambda Expression Over a List in C#
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment.
Dinesh GabhanePosted Nov 12, 2019, 11:56 PM
Very Good Article
Himani PatelPosted Feb 16, 2016, 2:37 PM
thank you so much it really helped me a lot :)
ricardo jrPosted May 17, 2013, 1:15 PM
Hi Hermant, thanks for the help! But my problem is that I need analyze a given string like "data > "\"17/05/2013\"" or "code <= "\"123456\"", "data" and "code" can be string attributes from a class, and can be from different classes. After analyzing the string I need make a lambda expression that execute the filtering. For example, I have a class public class Element { public string Name; public double Number; public string data; } var items = new List<Element>() { new Element("a", 1000 ,"13-12-2012 10:47:00:000"), new Element("b", 900 ,"13-12-2012 10:48:00:345"), new Element("b", 800 ,"13-12-2012 10:48:00:545"), new Element("d", 700 ,"13-12-2012 11:48:00:545"), new Element("e", 600 ,"13-12-2012 12:48:00:545"), new Element("x", 500.5 ,"13-12-2012 14:48:00:545"), new Element("y", 400 ,"13-12-2012 15:48:00:545"), new Element("z", 300 ,"13-12-2012 16:48:00:545") }; and for filter on Number I do: string s = "Number >= 500.50 && Number < 1000"; var pred = SimpleExpression.PredicateParser<Element>.Parse(s); var f = pred.Compile(); Now q has my filtered list by query: var q = from e in items where f(e) select e; The SimpleExpression.PredicateParser fails when analize "data > "\"17/05/2013\"" because both are strings. My work to do is make a analizer that generate a expression that can be used on var q = from e in items where f(e) select e; f(e) returns bool, so, if I use IComparer and IComparable I only can return int from Compare and CompareTo. The predicate parser that I using is from http://www.codeproject.com/Tips/355513/Invent-your-own-Dynamic-LINQ-parser thanks!
Hemant SrivastavaPosted May 17, 2013, 12:34 AM
Hi Ricardo, In that situation, You need to implement IComparable interface for Person class. Refer to following link http://support.microsoft.com/kb/320727
ricardo jrPosted May 16, 2013, 1:09 PM
Hi, I trying to do something like list all SSN >= "203456879", for example. My problem is that both types are strings and comparison is different than numeric types. Does someone have a clue?
wen zePosted Dec 10, 2012, 2:07 AM
Thanks for your sharing.
Gaurav GuptaPosted Oct 17, 2012, 2:14 AM
Thanks for sharing. I read that...
Hemant SrivastavaPosted Oct 15, 2012, 10:46 AM
@Gaurav Gupta: How to use lambda expression on a DataTable object, you can see my new article at: http://www.c-sharpcorner.com/UploadFile/0f68f2/querying-a-data-table-using-select-method-and-lambda-express/
Hemant SrivastavaPosted Oct 11, 2012, 9:20 PM
@shweta garg: Hi Shweta, Wherever we are getting a list of objects after using lambda expression, we could use "Count" property to get the number of occurances ... For example: listPersonsInCity.FindAll(e => (e.Age >= 60)).Count
Hemant SrivastavaPosted Oct 11, 2012, 8:18 PM
@Gaurav Gupta: ..... Thanks Gaurav.. Yes we can use lambda expression on data table object. I am going to write another article of using lamda expression over Data table...Wait for a few days ....
shweta gargPosted Oct 11, 2012, 12:53 AM
Nice explanation Sir....Sir can we count the number of occurrences of each value in the list?
Megha GoyalPosted Oct 11, 2012, 12:27 AM
It is easily understandable article. By this we come to know that how to perform search operation on list using lamda expression.Really very impressive
Tina GargPosted Oct 10, 2012, 11:42 PM
Very nice explanation Hemant Sir..It is very useful for me.Thanks
Gaurav ChauhanPosted Oct 10, 2012, 11:23 PM
Very useful article.
lavisha rastogiPosted Oct 10, 2012, 11:20 PM
nice article.....
Varesh TuliPosted Oct 10, 2012, 11:08 PM
Nice article to understands lambda. Thanks for sharing.
Gaurav GuptaPosted Oct 10, 2012, 10:57 PM
Good article for learn about Lambda. But can you show how to use Lambda experession with DataTable object.