LINQ options.loadwith problem

前端 未结 3 2204
梦谈多话
梦谈多话 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:06

    The problem in your case is Take(10). Here's from the horse's mouth:

    https://connect.microsoft.com/VisualStudio/feedback/details/473333/linq-to-sql-loadoptions-ignored-when-using-take-in-the-query

    The suggested workaround is to add Skip(0). That did not work for me, but Skip(1) did work. Useless as it may be, at least I know where my problem is.

    0 讨论(0)
  • 2021-02-15 12:19

    Set the EnabledDefferedLoad on your datacontext class to false.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题