开发者

Simple LINQ to SQL extension method

How would I write a simple LINQ to SQL extension method called "IsActive" which would contain a few basic criteria checks of a few different fields, so that I could reuse this "IsActive" logic all over the plac开发者_运维技巧e without duplicating the logic.

For example, I would like to be able to do something like this:

return db.Listings.Where(x => x.IsActive())

And IsActive would be something like:

public bool IsActive(Listing SomeListing)
{
    if(SomeListing.Approved==true && SomeListing.Deleted==false)
        return true;
    else
        return false;
}

Otherwise, I am going to have to duplicate the same old where criteria in a million different queries right throughout my system.

Note: method must render in SQL..


Good question, there is a clear need to be able to define a re-useable filtering expression to avoid redundantly specifying logic in disparate queries.

This method will generate a filter you can pass to the Where method.

public Expression<Func<Listing, bool>> GetActiveFilter()
{
  return someListing => someListing.Approved && !someListing.Deleted;
}

Then later, call it by:

Expression<Func<Filter, bool>> filter = GetActiveFilter()
return db.Listings.Where(filter);

Since an Expression<Func<T, bool>> is used, there will be no problem translating to sql.


Here's an extra way to do this:

public static IQueryable<Filter> FilterToActive(this IQueryable<Filter> source)
{
  var filter = GetActiveFilter()
  return source.Where(filter);
}

Then later,

return db.Listings.FilterToActive();


You can use a partial class to achieve this.

In a new file place the following:

namespace Namespace.Of.Your.Linq.Classes 
{
    public partial class Listing 
    {
        public bool IsActive()
        {
            if(this.Approved==true && this.Deleted==false)
                return true;
            else
                return false;
        }
    }
}

Since the Listing object (x in your lambda) is just an object, and Linq to SQL defines the generated classes as partial, you can add functionality (properties, methods, etc) to the generated classes using partial classes.

I don't believe the above will be rendered into the SQL query. If you want to do all the logic in the SQL Query, I would recommend making a method that calls the where method and just calling that when necessary.

EDIT

Example:

public static class DataManager
{
    public static IEnumerable<Listing> GetActiveListings()
    {
        using (MyLinqToSqlDataContext ctx = new MyLinqToSqlDataContext())
        {
            return ctx.Listings.Where(x => x.Approved && !x.Deleted);
        }
    }
}

Now, whenever you want to get all the Active Listings, just call DataManager.GetActiveListings()


public static class ExtensionMethods
{
  public static bool IsActive( this Listing SomeListing)
  {
    if(SomeListing.Approved==true && SomeListing.Deleted==false)
        return true;
    else
        return false;
  }
}


Late to the party here, but yet another way to do it that I use is:

public static IQueryable<Listing> GetActiveListings(IQueryable<Listing> listings)
{
    return listings.Where(x => x.Approved && !x.Deleted);
}

and then

var activeListings = GetActiveListings(ctx.Listings);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜