随笔 - 19  文章 - 0  评论 - 82  阅读 - 14970

wordpress 复杂的表结构。。

复制代码
View Code
 public class Post
    {
        public int ID { get; set; }
        public User author { get; set; }
        public DateTime post_date { get; set; }
        public DateTime post_date_gmt { get; set; }
        public string post_content { get; set; }
        public string post_title { get; set; }
        public string post_status { get; set; }
        public string comment_status { get; set; }
        public string ping_status { get; set; }
        public string post_password { get; set; }
        public string post_name { get; set; }
        public string to_ping { get; set; }
        public string pinged { get; set; }
        public DateTime post_modified { get; set; }
        public DateTime post_modified_gmt { get; set; }
        public string post_content_filtered { get; set; }
        public int post_parent { get; set; }
        public string guid { get; set; }
        public int menu_order { get; set; }
        public string post_type { get; set; }
        public string post_mime_type { get; set; }
        public int comment_count { get; set; }
        public List<Term_relationships> Term_relationships { get; set; }
    }
    public class Term
    {
        public int id { get; set; }
        public string name { get; set; }
        public string slug { get; set; }
        public int term_group { get; set; }
        //public virtual ICollection<Term_taxonomy> termList { get; set; }
    }
    public class Term_taxonomy
    {
        public int id { get; set; }
        public virtual Term term { get; set; }
        public string taxonomy { get; set; }
        public string description { get; set; }
        public int parent { get; set; }
        public int count { get; set; }
        public virtual ICollection<Term_taxonomy> relationships { get; set; }
    }
    public class Term_relationships
    {
        public int id { get; set; }
        public Post post { get; set; }
        public Term_taxonomy term_taxonomy { get; set; }
        public int term_order { get; set; }
        public virtual ICollection<Term_taxonomy> taxonomys { get; set; }
    }
复制代码

用EF实现查询
根据 分类查询 文章

错误的

      var posts = DataContext.Posts.
                Include(p => p.Term_relationships.Select(qs => qs.term_taxonomy).Select(pas => pas.term).Where(psss => psss.slug == cat));


这样是可以关联出数据来 可是隐藏的太深了

 var term = DataContext.Terms.Where(p => p.slug == cat)
                .Include(x=>x.termList.Select(s=>s.relationships.Select(p=>p.post)));


最终想得到的数据(缺少where)

  PagedList<Post> orders = DataContext.Posts.Include("Term_relationships")
                .Include("Term_relationships.term_taxonomy")
                .Include("Term_relationships.term_taxonomy.term")
                .OrderBy(p => p.post_date)
                .ToPagedList(id ?? 1, 5);

//高手给出的解决方案!

.Where(x => x.Term_relationships.Select(r => r.term_taxonomy.term.slug).Contains(cat))

posted on   蔚蓝海  阅读(227)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
< 2012年8月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示