locked
Linq to Entities for IQueryable error on custom property RRS feed

  • Question

  • Hi,

    I have a linq-to-entities query used to load a pageable grid:

    IQueryable BulkOrderProjects = new ProjectRepository().GetProjects()
        .Where(p => p.ProjectType.ProjectTypeID == (int)ProjectTypes.BulkProject &&
            p.ProjectStatusCode != completedStatus)
        .Select(p => new BulkOrderProjectViewModel
        {
            ProjectID = p.ProjectID,
            ProjectName = p.ProjectName,
            ItemExpectedCostTotal = p.ItemExpectedCostTotal ?? 0,
            ItemSellPriceTotal = p.ItemSellPriceTotal ?? 0,
            ProjectPriceTotal = p.ProjectPriceTotal,
            CreatedDate = p.CreatedDate,
            LastModifiedDate = p.LastModifiedDate,
            ProjectStatusCode = p.ProjectStatusCode,   
            MarginDollars = p.MarginDollars
        }
    );

    that is returning an error:

    "The specified type member 'MarginDollars' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported."

    Because MarginDollars is a CustomProperty of Entity Project, I created an expression for my getter in the Project entity's partial class:

    public static Expression<Func<Project, decimal>> MarginDollarsExpression
    {
        get { return p => p.ItemSellPriceTotal ?? 0 - p.ItemExpectedCostTotal ?? 0; }
    }

    public decimal MarginDollars
    {
        get
        {
            return MarginDollarsExpression.Compile()(this);
        }
    }

    But I still get the error.  Is there a way I can use this custom entity property in an IQueryable scenerio?  I would really like to have the benefits of not retrieving the entire dataset on each grid page load, etc.

    Thanks


    Dave Nordin

    • Moved by Bob Shen Monday, October 1, 2012 6:03 AM (From:Visual C# General)
    Sunday, September 30, 2012 2:58 PM

All replies

  • Interesting, I think custom properties can never be used or appear in Linq to entities query because such query is mapped to SQL.

    To workaround this, you need to select data with Linq to entities, then call ToList or AsEnumerable and than select properties MarginDollars in Linq to objects.

    Hope this helps you.


    Mark it as helpful if so!!! thanks, Mithilesh

    Sunday, September 30, 2012 8:32 PM
  • Tuesday, October 2, 2012 6:57 AM
  • Hi Dave,

    Any updates on this thread? If the problem remains, please feel free to let me know.

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, October 9, 2012 9:19 AM