LINQ options.loadwith problem

前端 未结 3 2228
梦谈多话
梦谈多话 2021-02-15 11:39

I am writing a tag-based ASP.net system. Using the following db scheme:

Topic TagTopicMap Tag

Basically it is a

3条回答
  •  一整个雨季
    2021-02-15 12:30

    The short answer is: LinqToSql has several quirks like this, and sometimes you have to use work-arounds...

    The Linq2Sql LoadWith option simply causes an inner join between the database tables, so you can force similar behavior by rewritting your Linq statement to something like (please forgive any typos, I'm used to writting Linq in VB syntax...):

    var x = from topic in db.Topics
            join topicMap in topic.TagTopicMaps
            orderby topic.dateAdded descending
            group topicMap by topicMap.topic into tags = Group;
    

    This syntax may be horribly wrong, but the basic idea is that you force Linq2Sql to evaluate the join between Topics and TagTopicMaps, and then use grouping (or "group join", "let", etc.) to preserve the object heirarchy in the result set.

提交回复
热议问题