lambda

Expression.Condition(nullableType.HasValue, new classInstance(){ ... }, null) can it be done some other way?

I am working on a projection utility and have one last (more?) hurdle to clear... Here is the scenario: public class Person { public string FirstName { get; set; } public string LastName { get; set; } public int? AddressID { get; set; } public Address Address { get; set; } public string Otherproperty1 { get; set; } ...

Convert a lambda to a dotted string e.g. x => x.Address.City becomes Address.City

I want to convert a lambda expression referencing a property or sub-property of an object, e.g. x => x.Address.City into a string Address.City. Is there an existing framework method (MVC, EF, ...?) that does this or do I need to roll my own? If the latter, any code out there already that does this? ...

IQueryable Lambda Projection Syntax

I have an IQueryable whose Entity Framework 4 objects I would like to project to their DTO equivalents. One such object 'Person' is an EF4 class, and the corresponding POCO PersonP is a class I've defined. I am using Automapper to map between them. However, when I try the following code: IQueryable<Person> originalModel = _repo.QueryAll...

selecting a distinct count from LINQ Stored Proc

Hi there I have a stored proc that returns a resultset thus: testID(guid), testName, outcomeID(guid), outcomeName - fields testGuid1, testName1, OutcomeGuid1, outcome1 testGuid1, testName1, OutcomeGuid2, outcome2 testGuid1, testName1, OutcomeGuid3, outcome3 testGuid1, testName1, OutcomeGuid4, outcome4 testGuid1, testName1, Outcome...

How to access property of generic type in linq expression

Iam using .NET 3.5. I have asp.net mvc app. There is base controller: public abstract class BackendController<TModel> : BaseController where TModel : class { // skipped ... public ActionResult BatchDelete(int[] ids) { var entities = repository.GetList().Where(item => ids.Contains(item.ID)); repository.delete(entities) } ...

Optional method arguments in C++, but from the callee's side?

In C++ methods can have optional arguments, like this: void myFunction (int arg1, int arg2=0); In this case myFunction can be called with 1 integer or with two. If you omit the second integer, the value 0 is passed instead. I am now looking for ways to obtain the same functionality, but from the callee's side. Suppose I have an int...

C# - how to pass 'out' parameter into lambda expression

I have a method with the following signature: private PropertyInfo getPropertyForDBField(string dbField, out string prettyName) In it, I find the associated value prettyName based on the given dbField. I then want to find all properties, if any, that have the name prettyName, so I'm trying to do the following: IEnumerable<PropertyIn...

Creating a queryable property for a linq to sql entity

I have a simple table structure in SQL Server: One table storing a list of Bikes (Bikes) and a second table storing a historical list of the bike's owners (Owners). Using Linq 2 SQL I generated a model that gives me a 'Bike' entity with a 1 to many relationship to 'Owner' called 'HistoricalOwners'. So far so good... Now I have many q...

Using object property for a key in dictionary

Hi I would like to use an objects property as the key for a dictionary. Can this be done? The ultimate goal for this is to use this so can see if the property is locked or not, in various states that an object can be in. These locked value is not persisted, just exist in the business rules for the model. Ideal code to see if field is ...

VB.Net - "With" and Closures don't mix

Just thought I'd share this in case anyone else has run into this. I did something similar today and it took me a while to figure out why this was causing a problem at runtime. This code: Public Class foo Public bar As String = "blah" End Class Public Sub DoInline() Dim o As New foo Dim f As Func(Of String) With o f = Func...

Entity Framework 4: How to turn a string into an object for .OrderBy(p => p.fieldname)?

Now this is a trick question because you cannot do this: var a = myDB.Where(p => p.field == "filter").OrderBy("it." + fieldname); You could change the Where to accept a string, which allows you to change the OrderBy to accept a string, but that's not the question. How can you turn a string like "productID, productName" in to an Order...

Specify code contract on Func<T> parameters?

Say I have the following public T Example(Func<T> f) { Contract.Requires(f != null); Contract.Requires(f() != null); // no surprise, this is an error ... } Is there any way to specify that my Func<T> parameters must obey some contracts? ...

Return from a C++0x lambda caller directly

I've just rewritten the following C89 code, that returns from the current function: // make sure the inode isn't open { size_t i; for (i = 0; i < ARRAY_LEN(g_cpfs->htab); ++i) { struct Handle const *const handle = &g_cpfs->htab[i]; if (handle_valid(handle)) { if (handle->ino == (*inode)->i...

c# List<string> to Lambda Expression with starter example: Refactor to handle the List

I have this: List<string> fields; fields[0] = "firstFieldName"; fields[1] = "secondFieldName"; ... fields[n] = "nthFieldName"; I want to get this: var selector = p => new {p.firstField, p.secondField, ..., p.nthFieldName} // selector is of type Expression<Func<Entity, object>> GoofBallLogic had this code that was simliar, ending ...

Turn "fieldname .startsWith. 'fieldValue'" into .Where(p => p.fieldname.StartsWith('fieldValue')) predicate?

I attempted to do this below, with mind blanks as it got complicated. I need to build a predicate to pass to my Product Repository from a client string: "fieldname .startsWith. 'fieldvalue'" "fieldname .contains. 'fieldvalue'" "fieldname .equals. 3" "fieldname .greaterThan. 3" "fieldname .lessThan. 3" The field names could be any exi...