lambda

Convert or map a class instance to a list of another one by using Lambda or LINQ?

I have the following two classes: class MyData { public List<string> PropertyList { get; private set;} public string PropertyB { get; set; } public string PropertyC { get; set; } } class MyData2 { public string PropertyA { get; set;} public string PropertyB { get; set; } public string PropertyC { get; set; } } If I have a...

How can I pass a property of a class as a parameter of a method in c#

I have a class that has a dozen or so properties that represent various financial fields. I have another class that needs to perform some calculations on each of those fields separately. The code inside those calculation methods are identical except for the field that it does the calculation on. Is there a way that I can pass a property...

Convert or map a list of class to another list of class by using Lambda or LINQ?

The question and answer of converting a class to another list of class is cool. How about to convert a list of MyData to another list of MyData2? For example: List<MyData> list1 = new List<MyData>(); // somewhere list1 is populated List<MyData2> list2; // Now I need list2 from list1. How to use similar LINQ or Lambda to get list2 = ... ...

Check if a value is in a collection with LINQ

Hello, I have a class "Employee", this has an IList<> of "TypeOfWork". public class Employee { public virtual IList<TypeOfWork> TypeOfWorks { get; set; } } public class TypeOfWork { public virtual Customer Customer { get; set; } public virtual Guid Id { get; set; } public virtual string Name{ get; set; } public vi...

What is the Efficiency and Performance of LINQ and Lambda Expression in .Net?

I have used .Net 3.5 and VS 2008 for more than a month. Like most .Net developers, I have evolved from years experience in .Net 1.0 & 2.0 and VS 2005. Just recently, I discovered the simplicity and power of LINQ and Lamda Expressions, as in my recent questions such as Find an item in list by LINQ, Convert or map a class instance to a lis...

How to get distinct instance from a list by Lamba or LINQ

I have a class like this: class MyClass<T> { public string value1 { get; set; } public T objT { get; set; } } and a list of this class. I would like to use .net 3.5 lambda or linq to get a list of MyClass by distinct value1. I guess this is possible and much simpler than the way in .net 2.0 to cache a list like this: List<MyC...

What is the purpose of lambda in this example?

lambda is used in this example in both the compose and the hydrate methods. What does lambda do here? def compose *lambdas if lambdas.empty? lambda { nil } elsif lambdas.size == 1 lambdas.first else lambda do |n| lambdas.first.call(compose(*lambdas[1..-1]).call(n)) end end end def hydrate(modulus, printabl...

Remove instances from a list by using LINQ or Lambda?

Now I come a stage to get all my data as a list in cache(objects) and my next thing I have to do is to remove some instances from the list. Normally, I would do removing like this: List<T> list; List<T2> toBeRemovedItems; // populate two lists foreach(T2 item in toBeRemovedItems) { list.Remove(delegate(T one) { // build a condit...

expression trees linq get value of a parameter ?

AddOptional<tblObject>(x =>x.Title, objectToSend.SupplementaryData); private static void AddOptional<TType>(Expression<Func<TType,string>> expr, Dictionary<string, string> dictionary) { string propertyName; string propertyValue; Expression expression = (Expression)expr; while (expression.NodeType == ExpressionType.Lambd...

Question about LINQ and Lambda requery

I have a question about the result of LINQ and Lambda query. For example, I have the following codes: class ClassA<T> { public string Name { get; set; } public T ObjectT { get; set; } } List<ClassA<T>> list; // list is populated // First way to get instance from list, reference type? ClassA<T> instance1 = list.Select(x=> x).W...

How to merge a list of lists with same type of items to a single list of items?

The question is confusing, but it is much more clear as described in the following codes: List<List<T>> listOfList; // add three lists of List<T> to listOfList, for example /* listOfList = new { { 1, 2, 3}, // list 1 of 1, 3, and 3 { 4, 5, 6}, // list 2 { 7, 8, 9} // list 3 }; */ List<T> l...

What does "=>" mean?

Forgive me if this screams newbie but what does "=>" mean in c#? I was at a presentation last week and this operator (I think) was used in the context of ORM. I wasn't really paying attention to the specifics of syntax until I went back to my notes. I'm a vb.net guy trying to do more in C#. I already tried Google and some of my referenc...

recursive lambda-expressions possible?

I'm trying to write a lambda-expression that calls itself, but i can't seem to find any syntax for that, or even if it's possible. Essentially what I wanted to transfer the following function into the following lambda expression: (I realize it's a silly application, it just adds, but I'm exploring what I can do with lambda-expressions i...

C++0x lambdas coding style

I wonder how people are using C++0x lambdas, in terms of coding style. The most interesting question is how thorough to be when writing the capture list. On one hand, the language allows to list captured variables explicitly, and by the "explicit is better than implicit rule", it would therefore make sense to do an exhaustive listing to ...

How can I convert this C# 2 delegate example to C# 3 lambda syntax?

I've got this example where I convert a C# 2 delegate example: Predicate<string> predicate2 = delegate(string n) { return n.StartsWith("J"); }; IList<string> namesWithJ2 = Tools.Filter(names, predicate2); Tools.Dump(namesWithJ2); to C# 3 lambda syntax example: var filteredNames = Tools.Filter(names, n => n.StartsWith("J")); Tool...

C# Anonymous function-scope

var foo = "bar"; new Func<String>(() => { var foo = ""; // This can't be done in C#. Why is that? /* In JavaScript, this is perfectly valid, since this scope (the anonymous function) is disconnected from the outer scope, and any variable declared within this scope will not affect variables in the outer scope */ ...

Can I get any terser with lambdas?

I'm writing a lot of code with lambdas these days. return _schema.GetAll<Node>() .ToList() .FindAll(node => node.Type == NodeType.Unmanaged) .Cast<Shape>() .ToList(); Note: GetAll() returns an IList. Can i get any terser? ...

How does Func<T,TResult> Work?

I am creating a Distinct extension method where I can pass in the criteria like the following. persons.Distinct(p => p.Name); I got the code from the web but I am having a hard time understanding the purpose of Func. Also, when I say p=> p.Name Am I sending the String Name or Am I sending the complete Person object. Here is the new D...

Can you refactor this ?

Can you refactor this ? private void ClearUserDataFields() { var textBoxes = this.Controls.OfType<TextBox>(); foreach (var txtBoxControl in textBoxes) { txtBoxControl.Text = String.Empty; } } Isn't there anyway to do this in one LOC ? ...

Predicates and lambda expression

I have recently moved to .net 3.0 (windows forms, C#). I want to know more about predicates and lambda expressions. Where should we use them? Do they improve performance? and how do they work internally. Thanks. ...