Pass Linq Expression to a function

Posted by Kushan Hasithe Fernando on Stack Overflow See other posts from Stack Overflow or by Kushan Hasithe Fernando
Published on 2012-04-03T05:26:38Z Indexed on 2012/04/03 5:28 UTC
Read the original article Hit count: 262

Filed under:
|
|
|

I want to pass a property list of a class to a function. with in the function based on property list I'm going to generate a query. As exactly same functionality in Linq Select method. Here I'm gonna implement this for Ingress Database.

As an example,

in front end I wanna run a select as this,

My Entity Class is like this

public class Customer
{
    [System.Data.Linq.Mapping.ColumnAttribute(Name="Id",IsPrimaryKey=true)]
    public string Id { get; set; }

    [System.Data.Linq.Mapping.ColumnAttribute(Name = "Name")]
    public string Name { get; set; }

    [System.Data.Linq.Mapping.ColumnAttribute(Name = "Address")]
    public string Address { get; set; }

    [System.Data.Linq.Mapping.ColumnAttribute(Name = "Email")]
    public string Email { get; set; }

    [System.Data.Linq.Mapping.ColumnAttribute(Name = "Mobile")]
    public string Mobile { get; set; }
}

I wanna call a Select function like this,

var result = dataAccessService.Select<Customer>(C=>C.Name,C.Address);

then,using result I can get the Name and Address properties' values.

I think my Select function should looks like this,

( *I think this should done using Linq Expression. But im not sure what are the input parameter and return type. * )

Class DataAccessService
{
   // I'm not sure about this return type and input types, generic types.
   public TResult Select<TSource,TResult>(Expression<Func<TSource,TResult>> selector)
   {
        // Here using the property list, 
        // I can get the ColumnAttribute name value and I can generate a select query.
   }
}

This is a attempt to create a functionality like in Linq. But im not an expert in Linq Expressions.

There is a project call DbLinq from MIT, but its a big project and still i couldn't grab anything helpful from that.

Can someone please help me to start this, or can someone link me some useful resources to read about this.

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ