none
Strange case RRS feed

  • Question

  • Hi all,

    I need help on this. I am developing a small CMS and using L2SQL for data access. Data layer may change in future so I chose a provider model. My CMS entities are ordinary classes like CmsCollection, CmsContent, CmsUser etc. All these classes have equivalent L2SQL objects defined in DBML. I also have repository classes to query or update db tables. What I am trying to do is this:

    I have a generic query method defined on my repository class, it goes like this:

    IQueryable<T> Query<T>(Expression<Func<T, bool>> predicate) where T : CmsEntity

    CmsEntity is the base of all Cms entities. There are also implicit operators defined in the code, so every dbml object can be implicitly converted to Cms equivalent classes.

    I want to run "predicate" on the underlying DBML with Linq but T is CmsEntity and there is no such thing as CmsEntity in the dbml file.

    I tried following code in the provider:

    public IQueryable<T> Query<T>(Expression<Func<T, bool>> predicate) where T : CmsEntity
    {
         var targetType = TypeMappings.Types[typeof(T)];
         return db.GetTable(targetType).Provider.CreateQuery(predicate).Cast<T>().AsQueryable();
    }
    TypeMappings class does the necessary type casts, from CmsEntity classes to DBML classes.

    But even I can't guess what this should do :)

    Can you think of a way?
    Monday, May 18, 2009 2:24 PM

All replies

  • Can't you just go:

    public IQueryable<T> Query<T>(Expression<Func<T, bool>> predicate)
    {
       return db.GetTable<T>.Where (predicate);
    }

    or am I missing something?



    Write LINQ queries interactively - www.linqpad.net
    Monday, May 18, 2009 3:07 PM
    Answerer