lambda

LinqToSql strange behaviour

I have the following code: var tagToPosts = (from t2p in dataContext.TagToPosts join t in dataContext.Tags on t2p.TagId equals t.Id select new { t2p.Id, t.Name }); //IQueryable tag2postsToDelete; foreach (Tag tag in tags) { Debug.WriteLine(tag); tagToPosts ...

Lambda question

I am trying to learn lambda in C# 3, and wondering how this function would be written using lambdas: Say you have a collection of Point3 values. For each of these points, p: create a new p, where .Y is: Math.Sin ((center - p).Length * f) center and f are external variables to be provided to the function. Also Point3 type will have ...

Does LINQ and Lambda expressions reduce Cyclomatic-complexity?

Does LINQ and Lambda expressions reduce cyclomatic-complexity? Just curious because CodeRush actually shows a reduction in cc when the VS analyzer increases it. ...

PrincipalSearchResult<Principal>, Lamda Expressions

Ladys and Gents, Im trying to filter out enteties from a principalcollection using what I think is called lamda expressions. I cant get this to work, I get no results. user.GetGroups() returns all the groups where user is member, but user.GetGroups().Where(....) does not return anything. Lets say that userprincipal user = Administrato...

What is the smoothest, most appealing syntax you've found for asserting parameter correctness in c#?

A common problem in any language is to assert that parameters sent in to a method meet your requirements, and if they don't, to send nice, informative error messages. This kind of code gets repeated over and over, and we often try to create helpers for it. However, in C#, it seems those helpers are forced to deal with some duplication fo...

Retrieving Property name from lambda expression

Is there a better way to get the Property name when passed in via a lambda expression? Here is what i currently have. eg. GetSortingInfo<User>(u => u.UserId); It worked by casting it as a memberexpression only when the property was a string. because not all properties are strings i had to use object but then it would return a unary...

when not to use lambda expressions

A lot of questions are being answered on stackoverflow, with members specifying how to solve these real world/time problems using lambda expressions. Are we overusing it, are we considering the performance impact of using lambda expressions? I found few articles that explores the performance impact of lambda vs anonymous delegates vs ...

Help a C# developer understand: What is a monad?

There is a lot of talk about monads these days. I have read a few articles / blog posts, but I can't go far enough with their examples to fully grasp the concept. The reason is that monads are a functional language concept, and thus the examples are in languages I haven't worked with (since I haven't used a functional language in depth)....

Using conditional lambda statements with a foreach Action on a list

Why Cant I do something like this? If I have a List<String> myList populated with items, I want to be able to act on each member in a conditional way like so: myList.ForEach(a => { if (a.Trim().Length == 0) a = "0.0"; }) But this will not compile. Im guessing its something to do with missing a return value? Im trying to prepare a li...

Change parameter from lambda function to lambda expression

Hello, I have extension method: public static IQueryable<TResult> WithFieldLike<TResult>( this IQueryable<TResult> query, Func<TResult, string> field, string value) { Expression<Func<TResult, bool>> expr = trans => field(trans).Contains(value); return query.Where(expr); } I need change parameter field to type: ...

How can I get every nth item from a List<T>?

I'm using .NET 3.5 and would like to be able to obtain every nth item from a List. I'm not bothered as to whether it's achieved using a lambda expression or LINQ. Edit Looks like this question provoked quite a lot of debate (which is a good thing, right?). The main thing I've learnt is that when you think you know every way to do som...

What is the best resource for learning C# expression trees in depth?

When I first typed this question, I did so in order to find the duplicate questions, feeling sure that someone must have already asked this question. My plan was to follow those dupe links instead of posting this question. But this question has not been asked before as far as I can see ... it did not turn up in the "Related Questions" li...

how to change the type of the parameter in an Expression?

since i am using POCOS in my domain, i want my repository to be able to received Expression filters of the type of my POCOS and change the parameter in the expression to be the of type of my LINQ tables, my fields have the same name as my members so i was able to accomplish this for 1 and 2 lambda conditions by breaking into members and ...

c# Lambda, LINQ .... improve this method

I am in the process of learning more about LINQ and Lambda expressions but at this stage, I simply don't "Get" Lambda expressions. Yes ... I am a newbie to these new concepts. I mean, every example I see illustrates how to add or subtract to parameters. What about something a little more complex? To help me gain a better understand...

Is it possible to declare and use an anonymous functon in a single statement?

Is there any way to combine the following two lines into a single statement? Func<XmlNode> myFunc = () => { return myNode; }; XmlNode myOtherNode = myFunc(); I've been trying things like the below but can't get it to work and can't determine from the documentation whether it should work or not? XmlNode myOtherNode = ((Func<XmlNode>) ...

Opportunities to use Func<> to improve code readability

Today I finally "got" the Func<> delegate and saw how I could use it to make some of my less readable LINQ queries (hopefully) more readable. Here's a simple code sample illustrating the above, in a (very) trivial example List<int> numbers = new List<int> { 1, 5, 6, 3, 8, 7, 9, 2, 3, 4, 5, 6, }; // To get the count of those that are l...

C# 3.0: Fill in objects with different behaviour in collections

I want to fill items in a combobox, each of them has different behaviour. Yes I know I could simply create 3 classes deriving from a base class. But my question is kind of "is there another way" and "what is possible". In Java one can do "new MyClass(){public void overriddenmethod(){...} }" but in C# we can not, can we? Now I use a lamb...

anyone tried new php beta release with anonymous functions?

Hi, has anybody tried out the new 5.3.0 RC1 php release and played a bit with anonymous functions? I would like to know if you can use it like python for functional programming. E.g., can you do something like: def sinus(x): if x<=0.1: return x else: return (lambda x: 3*x-4*x*x*x)(sinus(x/3)) print sinus(172.0) Or bet...

Dynamically created expressions

I'm creating a dynamic expression, which will order items in a list by some rule (lambda exp.). This is the code: Expression<Func<String, String>> exp = o => o; MethodCallExpression orderByExp = Expression.Call(typeof(Enumerable), "OrderBy", new Type[] { typeof(String), exp.Body.Type }, Expression.Parameter(typeof(IEnumerable<Strin...

[LINQ] Operations on Lambda Grouping

Hello, Im stuck with a LINQ group by situation trying to solve it without using foreach statement, here is the escenary: I Have two generic collections List<OrderHeader> and List<OrderDetail>, both have a same field "TOTRGVS" that contains total amount from a order, and the number of order is the key named "NDORGV". Then I want to foun...