Question

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 I wanna Iterate through the property list, which is passed from the caller.
        // 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.

Was it helpful?

Solution

What you're trying to do is creating a new anonymous type that consists of Name and Address. This is easily achievable via long form linq (I made that term up, for lack of a better explanation.) Here's a sample from Microsoft, link provided below:

public void Linq11() 
{ 
    List<Product> products = GetProductList(); 

    var productInfos = 
        from p in products 
        select new { p.ProductName, p.Category, Price = p.UnitPrice }; 

    Console.WriteLine("Product Info:"); 
    foreach (var productInfo in productInfos) 
    { 
        Console.WriteLine("{0} is in the category {1} and costs {2} per unit.", productInfo.ProductName, productInfo.Category, productInfo.Price); 
    } 
}

Details: Linq Select Samples

Update: So are you trying to do something like this then?

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

public object[] Select<TSource>(params Expression<Func<TSource, object>>[] selectors)
   {
       var toReturn = new object[selectors.Count()];

       foreach (var s in selectors)
       {
           var func = s.Compile();
           //TODO: If you implement Select a proper extension method, you can easily get the source
           toReturn[i] = func(TSource);
       }
        return toReturn;
   }

I don't understand why you're trying to implement Select as a function of DataAccessService? Are trying to create this as an extension method rather? If this is not what you mean though, you need to rephrase you're question big time and as one commenter suggested, tell us what you need not how you want us to design it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top