lambda

Rails scalar query

I need to display a UI element (e.g. a star or checkmark) for employees that are 'favorites' of the current user (another employee). The Employee model has the following relationship defined to support this: has_and_belongs_to_many :favorites, :class_name => "Employee", :join_table => "favorites", :association_foreign_key => "fav...

LINQ query and lambda expressions

I'm trying to write a LINQ query and am having problems. I'm not sure if lambda expressions are the answer or not but I think they may be. I have two combo boxes on my form: "State" and "Color". I want to select Widgets from my database based on the values of these two dropdowns. My widgets can be in one of the following states: Not S...

For loop index out of range ArgumentOutOfRangeException when multithreading

I'm getting some strange behavior... when I iterate over the dummyText List in the ThreadTest method I get an index out of range exception (ArgumentOutOfRangeException), but if I remove the threads and I just print out the text, then everything works fine. This is my main method: public static Object sync = new Object(); static void Ma...

InvalidOperationException (Lambda parameter not in scope) when trying to Compile a Lambda Expression

Hello, I'm writing an Expression Parser to make my API more refactor friendly and less error prone. basicaly, I want the user to write code like that: repository.Get(entity => entity.Id == 10); instead of: repository.Get<Entity>("Id", 10); Extracting the member name from the left side of the binary expression was straight forward....

Does C use lambda expressions?

And, if it does, how do you use one? (syntax) Also, why does or why doesn't C support lambda expressions? ...

Problem in populating a dictionary using Enumerable.Range()

If I do for (int i = 0; i < appSettings.Count; i++) { string key = appSettings.Keys[i]; euFileDictionary.Add(key, appSettings[i]); } It is working fine. When I am trying the same thing using Enumerable.Range(0, appSettings.Count).Select(i => { string Key = appSettings.Keys[i]; string Value = appSettings[i]; euFileDic...

lambda expression for a query on two tables that returns records from one table

I have two tables TableA (articles) int id int Type string name and TableB (compatibles) int linked_ID int tableA_ID TableA records: id=1, Type=0, name="ArticleA" id=2, Type=1, name="ArticleB" id=3, Type=2, name="ArticleC" id=4, Type=1, name="ArticleD" TableB records: linked_ID= 1, tableA_ID=2 linked_ID= 1, tableA_ID=3 linked_ID= 1...

parsing expression trees with booleans

I am trying to parse an expression tree for a linq provider and running into a little snag with booleans. I can parse this no problems. var p = products.Where(x=>x.IsAvailable == true).ToList(); however when its written like this? var p = products.Where(x=>x.IsAvailable).ToList(); i only get a MemberAccess to look at and i can't s...

How do I use a Lambda expression to sort INTEGERS inside a object?

I have a collection of objects and I know that I can sort by NAME (string type) by saying collEquipment.Sort((x, y) => string.Compare(x.ItemName, y.ItemName)); that WORKS. But I want to sort by a ID (integer type) and there is no such thing as Int32.Compare So how do I do this? This doesn't work collEquipment.Sort((x, y) => (x.ID ...

C# Joins/Where with Linq and Lambda

Hello, I'm having trouble with a query written in Linq and Lambda. So far, I'm getting a lot of errors here's my code: int id = 1; var query = database.Posts.Join(database.Post_Metas, post => database.Posts.Where(x => x.ID == id), meta => database.Post_Metas.Where(x => x....

Properly handling possible System.NullReferenceException in lambda expressions

Here's the query in question return _projectDetail.ExpenditureDetails .Where(detail => detail.ProgramFund == _programFund && detail.Expenditure.User == _creditCardHolder) .Sum(detail => detail.ExpenditureAmounts.FirstOrDefault( amount => amount.isCurrent && !amount.requiresAudit) .CommittedMonthlyRecord.Proj...

Getting error in MVC Proj while writing Lambda Expression

hi, i am creating a sample movie (MVC) application. I was getting fine with Viewing and Creating a new record, but when i wrote the code to get the details of a particular record i met with the following error: Unable to cast objec`t of type 'System.Data.Objects.ObjectQuery`1[MovieApp.Models.Movie]' to type 'MovieApp.Model`s.Movie'. ...

Dynamic "WHERE IN" on IQueryable (linq to SQL)

I have a LINQ to SQL query returning rows from a table into an IQueryable object. IQueryable<MyClass> items = from table in DBContext.MyTable select new MyClass { ID = table.ID, Col1 = table.Col1, Col2 = table.Col2 } I then want to perform a SQL "WHERE ... IN ...." query on the results. This works fine using the following. (return ...

Lambda expressions as CLR (.NET) delegates / event handlers in Visual C++ 2010

Is it possible to use the new lambda expressions in Visual C++ 2010 as CLR event handlers? I've tried the following code: SomeEvent += gcnew EventHandler( [] (Object^ sender, EventArgs^ e) { // code here } ); It results in the following error message: error C3364: 'System::EventHandler' : invalid argument for deleg...

How to create a function and pass in variable length argument list?

We can create a function p in the following code: var p = function() { }; if (typeof(console) != 'undefined' && console.log) { p = function() { console.log(arguments); }; } but the arguments are passed like an array to console.log, instead of passed one by one as in console.log(arguments[0], arguments[1], arguments[2], ... Is ...

Order a List (C#) by many fields?

Hi everyone, I want to order a List of objects in C# by many fields, not just by one. For example, let's suppose I have a class called X with two Attributes, A and B, and I have the following objects, in that order: object1 => A = "a", B = "h" object2 => A = "a", B = "c" object3 => A = "b", B = "x" object4 => A = "b", B = "b" and I ...

.NET - execute a lambda on another computer

Hi ;) I've recently implemented an IronRuby web service application for a client, to replace an existing C# .NET DLL. What the client forgot to mention was that in the mean time they implemented a new version of the DLL, with a new API based on lambda expressions. And made sure all calls (thousands :( ) use the new syntax. So now I need...

C# - System.StackOverflowException with Lambda

Under what situations would this code error out with System.StackOverflowException? Accounts.Sort((x, y) => string.Compare(x.AccountId, y.AccountId)); Update: the property is written as: public string AccountId { get { return _accountId; } set { _accountId = value; } } Nothing special going on at all. ...

Entity Framework - Condition on one to many join (Lambda)

Hi, I have 2 entities: Customer & Account, where a customer can have multiple accounts. On the account, I have a "PlatformTypeId" field, which I need to condition on (multiple values), among other criterions. I'm using Lambda expressions, to build the query. Here's a snippet: var customerQuery = from c in context.CustomerSet.Includ...

Get the property, as a string, from an Expression<Func<TModel,TProperty>>

I use some strongly-typed expressions that get serialized to allow my UI code to have strongly-typed sorting and searching expressions. These are of type Expression<Func<TModel,TProperty>> and are used as such: SortOption.Field = (p => p.FirstName);. I've gotten this working perfectly for this simple case. The code that I'm using for pa...