none
Querying with a proxy method. Linq-to-SQL. RRS feed

  • Question

  • Hi everybody...

    Now I've trie several times, but I cannot find a proper way to do it. My scenario is:

    public interface IBuildingUnit
    {
    IBuildingUnit AnotherBuildingUnitProxy { get; }
    }

    public partial class Wall : IBuildingUnit
    {
    // Wall is a partial with the rest of the code hidden away in the DataContext autogenerated designer file
    // It has the one association (for simplicity) named AnotherBuildingUnit that points to another Wall

    // I often in my program need to refer to this AnotherBuildingUnit only having knowledge about its interface
    // IBuildingUnit. Therefore I have the Proxy method shown above. It's implemented here:

    public IBuildingUnit AnotherBuildingUnitProxy
    {
    get { return (IBuildingUnit)AnotherBuildingUnit; }
    }
    }


    Here's my problem then, I want to make a query like this:

    // Implements an abstract in the base class
    protected abstract IQueryable<IBuildingUnit> buildingUnitsQuery()
    {
    return (from x in db.Walls select x).Cast<IBuildingUnit>();
    }

    // This one is implemented in the base class
    public IQueryable<IBuildingUnit> doQuery()
    {
    return  from bu in buildingUnitsQuery()
    where bu.AnotherBuildingUnitProxy != null
    select bu;
    }

    Is there any way to make the AnotherBuildingUnitProxy actually translate to SQL??? It only needs to map to the column name that it references in the generated SQL, but I can't by any means make it do that. Do you have any suggestions?

    Regards, Asger

    Wednesday, April 30, 2008 8:28 PM

Answers

  • Hi Asger,

    One way could be looking at implementing your own IQueryable provider. You need to write your own expression visitor.

    Matt Warren has a good series of post about it. I believe this is the one that will particularly help you:

    http://blogs.msdn.com/mattwar/archive/2007/07/31/linq-building-an-iqueryable-provider-part-ii.aspx


    Tuesday, June 3, 2008 10:03 AM

All replies

  •  

    I don't think it could happen. Even for System.String, it can only translate methods like Contains, BeginWith or EndWith. Not all the methods can be translated, and what's worse, you are using a virtual method. ^_^
    Tuesday, June 3, 2008 6:42 AM
  • Hi Asger,

    One way could be looking at implementing your own IQueryable provider. You need to write your own expression visitor.

    Matt Warren has a good series of post about it. I believe this is the one that will particularly help you:

    http://blogs.msdn.com/mattwar/archive/2007/07/31/linq-building-an-iqueryable-provider-part-ii.aspx


    Tuesday, June 3, 2008 10:03 AM