none
How to prevent a property being loaded from a compiled query RRS feed

  • Question

  • I have a LINQ to SQL class, PRODUCT, that contains a Binary property.

    I am using a compiled query to retrieve a collection of PRODUCTS:

    private static readonly Func<DataContext, short, IQueryable<PRODUCT>>
             SelectProducts_ByProductType = System.Data.Linq.CompiledQuery.Compile
            (
                    (DataContext DB, short PRODUCT_TYPE_ID) =>
                            from Product in DB.PRODUCTs
                            where Product.PRODUCT_TYPE_ID == PRODUCT_TYPE_ID
                            select Product
            );
    

    Is there a way to prevent the Binary property from being retrieved by the compiled query?

    Wednesday, February 3, 2010 3:51 PM

Answers

  • hi cander,

    are you going to use the binary property anywhere?  if not, you can avoid it being loaded from db by omitting the column attribute from Product Class.  If that is not an option and you might need it at other times, you can create another class and return subset into this class:


    public class MyProduct
    {
    ...
    }


    private static readonly Func<DataContext, short, IQueryable<MyProduct>>
             SelectProducts_ByProductType = System.Data.Linq.CompiledQuery.Compile
            (
                    (DataContext DB, short PRODUCT_TYPE_ID) =>
                            from Product in DB.PRODUCTs
                            where Product.PRODUCT_TYPE_ID == PRODUCT_TYPE_ID
                            select new MyProduct() {...}
            );

    regards,
    kashif
    • Marked as answer by cander Thursday, February 4, 2010 10:09 AM
    Thursday, February 4, 2010 8:32 AM

All replies

  • hi cander,

    are you going to use the binary property anywhere?  if not, you can avoid it being loaded from db by omitting the column attribute from Product Class.  If that is not an option and you might need it at other times, you can create another class and return subset into this class:


    public class MyProduct
    {
    ...
    }


    private static readonly Func<DataContext, short, IQueryable<MyProduct>>
             SelectProducts_ByProductType = System.Data.Linq.CompiledQuery.Compile
            (
                    (DataContext DB, short PRODUCT_TYPE_ID) =>
                            from Product in DB.PRODUCTs
                            where Product.PRODUCT_TYPE_ID == PRODUCT_TYPE_ID
                            select new MyProduct() {...}
            );

    regards,
    kashif
    • Marked as answer by cander Thursday, February 4, 2010 10:09 AM
    Thursday, February 4, 2010 8:32 AM
  • thanks Kpbutt,

    I had already tried creating a new PRODUCT{...} subset, which throws an exception Explicit construction of entity type 'PRODUCT' in query is not allowed. Creating another class to project the results into works fine. I have now created a new class with all the same properties of the PRODUCT class, plus a translation method to return a PRODUCT.

    It is a little annoying having to create another class to hold the results, meaning I will end up with two near identical classes for holding the same data. This almost seems to negate the advantage of having LINQ to SQL create these classes for me. Just out of curiosity, do you know why explicit construction of LINQ to SQL enities is not allowed in queries?
    Thursday, February 4, 2010 10:09 AM