Is it possible to add a calculated TotalXXX property to an entity with a relationship to a collection XXX? RRS feed

  • Question

  • I am using Astoria->EDM -> Linq to SQL.

    I have a collection of Foo entities, each Foo has a collection of Bars.

    I want retrieve my collection of Foo objects with a count of the number of Bars in each Foo without retrieving all of the Bar objects.  I would like to be able to do this in one round trip instead of individual $count calls on the bar collections, as each one would be a round-trip to just get the total.

    Thinking about this I can think of a couple of ways I might be able to achieve this:

    1. change the Foo EDM to bind to a new SQL view with a calculated TotalBars property.  I don't know what happens when I try to create a new Foo via EDM!?

    2. Change the Foo EDM to have a property TotalBars which gets it from the LinqToSQL.  Is this possible?  How do I do that? Again, what happens whn I try to create a new Foo?

    3.Perhaps I missed something with the new $count apis?

    4. Other ideas?


    Tuesday, July 7, 2009 6:07 PM


  • Hello Tom,

    Both 1 and 2 are possible. You can have a "DefiningQuery" in the SDL, and then map it to a corresponding conceptual model. "Importing a view" is essentially the same. You can define associations inside EF on the view you imported, but since it's a view, it'll be read only. When you create a new foo, you can only post it to the Foo collection, rather than the view itself.

    If all you are worrying about is network overhead for round tripping, you can consider batch operations. Currently $count does not count nested sets, that's something we could look into in the future.



    Peter Q. http://blogs.msdn.com/peter_qian
    Tuesday, July 7, 2009 10:05 PM