lambda

LINQ to SQL Wildcards

How can I build in wildcards to my LINQ To SQL lambda expression? This is what I have currently: var query = from log in context.Logs select log; foreach (string filter in CustomReport.ExtColsToFilter) { string tempFilter = filter; query = query.Where(Log => Log.FormattedMessage.Contains(tempFilter)); } This works fine up unt...

Strange LINQ to SQL Behavior

Here is my dataset: 1 David 2 David 3 Marc 4 Marc 5 Marc 6 Marc 7 Marc 8 Marc 9 Marc 10 Marc 11 Marc 12 Marc 13 Marc 14 Marc 15 Marc This query returns 2 records (correct): query = query.Where(Log => SqlMethods.Like (Log.FormattedMessage, "%<key>Name</key><value>David</value>%")); This query returns 2 records (correct): qu...

List filtering: list comprehension vs. lambda + filter

I happened to find myself having a basic filtering need: I have a list and I have to filter it by an attribute of the items. My code looked like this: my_list = [i for i in my_list if i.attribute == value] But then i thought, wouldn't it be better to write it like this? filter(lambda x: x.attribute == value, my_list) It's more rea...

What is the motivation behind c++0x lambda expressions?

I am trying to find out if there is an actual computational benefit to using lambda expressions in c++, namely "this code compiles/runs faster/slower because we use lambda expressions" OR is it just a neat development perk open for abuse by poor coders trying to look cool? Thanks. PS. I understand this question may seem subjective but ...

How to get XML into a Dictionary with an Expression?

I have the following XML: <PerformancePanel page="PerformancePanel.ascx" title=""> <FundGroup heading="Net Life Managed Funds"> <fund id="17" countryid="N0" index="24103723" /> <fund id="81" countryid="N0" index="24103723" /> <fund id="127" countryid="N0" index="24103722" /> <fund id="345" countryid="N0" i...

How-To Convert IQueryable Expression Tree to Lambda

Hi, How can i convert a IQueryable Expression Tree to a Expression<Func<T,bool>> Expression. IQueryable<Book> Books; var query = Books.Where(p => p.Author.AuthorId == 5); Expression<Func<Book, bool>> expression = ????? ...

Why is it not possible to evaluate lambdas in the immediate window?

Is there any particular reason? Is it not possible at all or is it just not implemented yet? Maybe there are any third-party addins that allow lambda evaluations? UPDATE: I've found this project on codeplex Extended Immediate Window. Seems that it has been abandoned for some time, but this can be a proof of a concept. Does anybody know...

Lambda Expressions in T4 Templates

Whilst putting together a T4 template I threw in a simple lambda expression: <#=string.Join(",", updateFields.ConvertAll(field => field.Name).ToArray())#> This causes the template to fail to generate with the error: Compiling transformation: Invalid expression term '>' On the line with the lambda expression. This has been check...

Is ILMerge broken when merging assemblies containing Lambdas in VS2010?

I've been trying to use ILMerge to merge assemblies in VS2010 and the resulting primary assembly ends up being unusable. This only seems to occur when the assemblies being merged contain methods with Lambda expressions. Also, it seems to work fine in VS2008. I've outlined my investigation in more detail at http://tinyurl.com/38mz4ef ...

C# Lambda Problem

Probably something simple, but as I'm new to lambda expressions, the problem evades me: m => m.contactID == contactID && m.primaryAddress == true && (m.addressTypeID == 2 || m.addressTypeID == 3) I tried to use that lambda expression but I receive an invalid operator. Is there a way to simplify this so that it would work? Edit: The ...

Use Lambda in Attribute constructor to get method's parameters

I'm not even sure if this is possible, but I've exhausted all of my ideas trying to figure this out, so I figured I'd send this out to the community and see what you thought. And, if it's not possible, maybe you'd have some ideas as well. I'm trying to make an Attribute class that I can add to a method that would allow me to use a lambd...

GridView ObjectDataSource LINQ Paging and Sorting using multiple table query.

I am trying to create a pageing and sorting object data source that before execution returns all results, then sorts on these results before filtering and then using the take and skip methods with the aim of retrieving just a subset of results from the database (saving on database traffic). this is based on the following article: http:/...

Is there such a thing as a MemberExpression that handles a many-to-many relationship?

We're trying to make it easy to write strongly-typed code in all areas of our system, so rather than setting var sortColumn = "FirstName" we'd like to say sortOption = (p => p.FirstName). This works great if the sortOption is of type Expression<Func<Person, object>> (we actually use generics in our code but that doesn't matter). However,...

How to replace auto-implemented c# get body at runtime or compile time?

I've been trying to figure this out all night, but I guess my knowledge of the .Net Framework just isn't that deep and the problem doesn't exactly Google well, but if I can get a nod in the right direction I'm sure I can implement it, one way or another. I'd like to be able to declare a property decorated with a custom attribute as such...

How can I use linq to build an object from 1 row of data?

Hi All, I have a quick linq question. I have a stored proc that should return one row of data. I would like to use a lambda to build an object. Here's what I'm currently doing which works, but I know I should be able to use First instead of Select except I can't seem to get the syntax correct. Can anyone straighten me out here? Thanks f...

List<object>.RemoveAll - How to create an appropriate Predicate

This is a bit of noob question - I'm still fairly new to C# and generics and completely new to predicates, delegates and lamda expressions... I have a class 'Enquiries' which contains a generic list of another class called 'Vehicles'. I'm building up the code to add/edit/delete Vehicles from the parent Enquiry. And at the moment, I'm sp...

reduce python list of objects to dict object.id -> object

Hello! You have list of objects each of them have id property. Here's my way to covert it to dict where keys are ids and values are objects: reduce( lambda x,y: dict(x.items() + { y.id : y}.items()), list, {} ) Suggest better way to do it. ...

MenuItemCollection vs. List<MenuItem>

Recently I wrote a piece of C# code utilizing a Lambda expression: var dynMenu = new List<MenuItem>(); // some code to add menu items to dynMenu if (!dynMenu.Any(x => x.Text == controller)) { // do something } While going thru my code, I discovered that each MenuItem itself has a property called ChildItems which happens to be of ...

Func<someType,someType=""> - What does the Equals sign mean?

Danny initially asked this question in response to a recent Scott Hanselman post: Who can tell me what's this : Func<Customer, bool=""> A optional parameter with default value? A empty string for a bool? I replace it with Func, and get the different result in my machine! Everything works well, I get "where" statement wi...

ObservableCollection slicing using lambda

I have ObservableCollection<ViewUnit> _myItems field, where ViewUnit implements INotifyPropertyChanged. ViewUnit has Handled : bool property. The main view of WPF application has a ListBox which binds to _myItems. I want a separate view of non-handled items only, that is, to have an IObservableCollection<> depended on existing _myItem...