lambda

How do you perform a left outer join using linq extension methods

Assuming I have a left outer join as such: from f in Foo join b in Bar on f.Foo_Id equals b.Foo_Id into g from result in g.DefaultIfEmpty() select new { Foo = f, Bar = result } How would I express the same task using extension methods? E.g. Foo.GroupJoin(Bar, f => f.Foo_Id, b => b.Foo_Id, (f,b) => ???) .Select(???) ...

MVC and lambda's for creating a record

Hi, I'm trying to add a record to a database. My Model is fairly simple: A Project table with a companyId field that associates to a Company table. Here's were I'm stuck.. var companyTemp = collection["company"]; var company = isspDB.Company.Where(co => co.companyId == 1).First(); What I basically need is: var company = isspDB.Com...

What does () mean in a lambda expression when using Actions?

I have pasted some code from Jon Skeet's C# In Depth site: static void Main() { // First build a list of actions List<Action> actions = new List<Action>(); for (int counter = 0; counter < 10; counter++) { actions.Add(() => Console.WriteLine(counter)); } // Then execute them foreach (Action action in ...

C# Select Distinct Values from IGrouping

I have two classes: class Foo { public Bar SomeBar { get; set; } } class Bar { public string Name { get; set; } } I have a list of Foos that I group together: var someGroup = from f in fooList orderby f.SomeBar.Name ascending group f by f.SomeBar.Name into Group select Gro...

Define backgroundworker's RunWorkerCompleted with an anonymous method?

I Hope I used the right term What I'm aiming for is something like this (I realise it doesn't work that way): private bool someBool = false; BackgroundWorker bg = new BackgroundWorker(); bg.DoWork += new DoWorkEventHandler(DoLengthyTask); bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler( ()=> { someBool = true; Log...

What is the best way to encapsulate Linq to SQL data access?

I've been trying to encapsulate the object mapping in a projects data repository. Perhaps EF will provide the level of abstraction required but for a range of reasons I am using Linq to SQL at the moment. The following code aims to return the users in the database as a list of ModUser objects, where ModUser is POCO that the repository ex...

How to create LINQ Expression Tree with anonymous type in it

I would like to generate the following select statement dynamically using expression trees: var v = from c in Countries where c.City == "London" select new {c.Name, c.Population}; I have worked out how to generate var v = from c in Countries where c.City == "London" select new {c.Name}; but I cannot ...

Setting/Removing event handlers in .Net

So I am stuck with fixing/maintaining another programmers code (blech) I am a firm professor of the rule "If it ain't broke dont fix it!" so depsite wanting to change something every time I come across horrendous code, I am keeping myself limited to only changing the absolute minimum amount of code possible to make the required fixes. ...

What is call/cc?

I've tried several times to grasp the concept of continuations and call/cc. Every single attempt was a failure. Can somebody please explain me these concepts, ideally with more realistic examples than these on Wikipedia or in other SO posts. I have background in web programming and OOP. I also understand 6502 assembly and had a minor ra...

DataTable.Select vs DataTable.rows.Find vs foreach vs Find(Predicate<T>)/Lambda

I have a DataTable/collection that is cached in memory, I want to use this as a source to generate results for an auto complete textbox (using AJAX of course). I am evaluating various options to fetch the data quickly. The number of items in the collection/rows in the datatable could vary from 10000 to 2,000,000. (So that we dont get di...

How to run an operation on a collection in Python and collect the results?

How to run an operation on a collection in Python and collect the results? So if I have a list of 100 numbers, and I want to run a function like this for each of them: Operation ( originalElement, anotherVar ) # returns new number. and collect the result like so: result = another list... How do I do it? Maybe using lambdas? ...

Difference between Expression.Call overloads?

Hi, I was attempting to dynamically create a Where predicate for a LINQ2SQL query: ...Where(SqlMethods.Like(r.Name, "%A%") || SqlMethods.Like(r.Name, "%B%") || SqlMethods.Like(r.Name, "%C%") || ...) A, B, C, etc. come from some array. So I tried the following: var roleExpression = Expression.Parameter(typeof(Role),...

LINQ: Dot Notation vs Query Expression

I am beginning to use LINQ in general (so far toXML and toSQL). I've seen that sometimes there are two or more ways to achieve the same results. Take this simple example, as far as I understand both return exactly the same thing: SomeDataContext dc = new SomeDataContext(); var queue = from q in dc.SomeTable where q.SomeDate <= ...

What does "() =>" mean in C#?

Came across the following line in the Composite Application Guidelines. I know the => is a lambda but what does the () mean? What are some other examples of this? What is it called so I can search for it? this.regionViewRegistry.RegisterViewWithRegion(RegionNames.SelectionRegion , () => this.container.Resolve<EmployeesListPre...

Why doesn't this lambda example from the MSDN site work?

What do I have to do to the following lambda example to get it to work? ERROR: Only assignment, call, increment, decrement, and new object expressions can be used as a statement http://msdn.microsoft.com/en-us/library/bb397687.aspx using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Linq....

C# SelectMany

I can flatten the results of a child collection within a collection with SelectMany: // a list of Foos, a Foo contains a List of Bars var source = new List<Foo>() { ... }; var q = source.SelectMany(foo => foo.Bar) .Select(bar => bar.barId) .ToList(); this gives me the list of all Bar Ids in the Foo List. When I attempt to g...

What is the fastest way to determine if a row exists using Linq to SQL?

I am not interested in the contents of a row, I just want to know if a row exists. The Name column is a primary key, so there will either be 0 or 1 matching rows. Currently, I am using: if ((from u in dc.Users where u.Name == name select u).Count() > 0) // row exists else // row doesn't exist While the above works, it does a l...

What is the correct C# lambda syntax here?

I'm trying to use lambda syntax to be able to swap out search rule engines at runtime. In the below example, what syntax do I need to be able to pass the name of the method to my GetSearchResults method so that the GetSearchResults method can use the appropriate logic to decide which results to return? string searchRulesMethodName = "S...

C# How to convert an Expression<Func<SomeType>> to an Expression<Func<OtherType>>

I have used C# expressions before based on lamdas, but I have no experience composing them by hand. Given an Expression<Func<SomeType, bool>> originalPredicate, I want to create an Expression<Func<OtherType, bool>> translatedPredicate. In this case SomeType and OtherType have the same fields, but they are not related (no inheritance and...

Are lambda expressions multi-threaded?

Are lambda expressions multi-threaded? Say when you write a mathematical formula as a lambda method, when you pass it to another method, would it be multi-threaded? ...