Print Page | Close Window

Generic Distinct Projection

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2465
Printed Date: 05-Feb-2026 at 7:38pm


Topic: Generic Distinct Projection
Posted By: CalvinC
Subject: Generic Distinct Projection
Date Posted: 24-Jan-2011 at 3:51pm

I have a large flat legacy table that has many columns.  I would like to write a generic method that can fetch the distinct values in any two columns:

IEnumerable<T1, T2> FetchUniqueValues<T1, T2>(

  Expression<Func<TheEntity, T1>> toTypeT1,

  Expression<Func<TheEntity, T2>> toTypeT2)

{

            //

}


Is there a clever way to accomplish this with DevForce?


Thanks!




Replies:
Posted By: sbelini
Date Posted: 25-Jan-2011 at 3:35pm

Hi Calvin,

 
This is a feature that will be available with DevForce2010 v6.0.9.0. (returning projections in a query built dynamically)
 
 
Right now you can build the query dynamically, but it will return a collection of the Entity type being queried:
 
  var expr1 = PredicateBuilder.Make(typeof(Employee), "LastName"
    , FilterOperator.StartsWith
    , "D");
  var expr2 = PredicateBuilder.Make(typeof(Employee), "Country"
    , FilterOperator.IsEqualTo
    , "USA");
  var expr12 = expr1.And(expr2);
  var exprFunc = (Expression<Func<Employee, bool>>)expr12.ToLambdaExpression();
  var query = mgr.Employees.Where(exprFunc);
  var results = query.Execute(); // results is a collection of Employee
You can always query your large table and return a projection with only the desired fields:
 
var query = mgr.Employees
  .Where(e => e.LastName.StartsWith("D") && Country.Equals("USA"))
  .Select(e => new {Name = e.FirstName, e.City});
  var results = query.Execute(); // results is a collection of projection { Name, City }
 
Note that upon execution a query will always return IEnumerable<T> where T is an Entity type or a projection.
 
Silvio.


Posted By: CalvinC
Date Posted: 25-Jan-2011 at 4:03pm
Thanks for the reply Silvio.

You're demo constructs the Where clause dynamically.  That's certainly useful; but it's not what I'm asking about.

I want to know if it's possible to construct a Select clause dynamically (an anonymous projection) and then subsequently append a Distinct clause.

I.e, is there a way to construct this line
    .Select(e => new {Name = e.FirstName, e.City})
dynamically (where FirstName and City are supplied as parameters)?



Posted By: sbelini
Date Posted: 25-Jan-2011 at 4:29pm
Hi Calvin,
 
Sorry if it wasn't clear in my previous post, but this feature is not available yet.
 
Like I previously mentioned, the above mentioned feature (returning projections in a query built dynamically) will be available with DevForce2010 v6.0.9.0.
 
The snippets I provided are only to demonstrate what DevForce can accomplish as of now.
 
Best regards,
   Silvio.



Print Page | Close Window