Exemplo n.º 1
0
        static ProblemRepository()
        {
            User yh1          = new User("28zhu", "mima1!"),
                 yh2          = new User("WhiteWater", "mima2!");
            Keyword CSharp    = new Keyword("C#"),
                    JAVA      = new Keyword("JAVA"),
                    Windows   = new Keyword("Windows"),
                    Linux     = new Keyword("Linux"),
                    CPlusPlus = new Keyword("C++");
            string title1     = "有一个自定义UI控件,此控件使用在不同的系统中会有不同的呈现,之前的做法是各种switch case,阅读代码时让人很难受,另外新创建一个用到此控件的系统,要修改代码的地方也多,只要有swich case 的地方都要再加一个case。请教一个好一些的方式来处理这个问题,目的是让代码更加清楚",
                   title2     = "手动导入jar包,运行报错的问题";
            string body1      = "RT,也不知道描述的清楚不清楚。求一个思路",
                   body2      = "运行就报这个错误,这个jar包我导入项目了的,不然编译都无法通过。";

            problems = new List <Problem>
            {
                new Problem(body1, yh1)
                {
                    Title    = title1,
                    KeyWords = new List <Keyword>
                    {
                        CSharp,
                        CPlusPlus,
                        JAVA
                    }
                },
                new Problem(body2, yh1)
                {
                    Title    = title2,
                    KeyWords = new List <Keyword>
                    {
                        CSharp,
                        Linux
                    }
                },
                new Problem(body2, yh2)
                {
                    Title    = title2,
                    KeyWords = new List <Keyword>
                    {
                        CSharp,
                        CPlusPlus,
                        Windows
                    }
                },
                new Problem(body1, yh1)
                {
                    Title    = title1,
                    KeyWords = new List <Keyword>
                    {
                        CSharp,
                        CPlusPlus,
                        JAVA
                    }
                },
                new Problem(body2, yh2)
                {
                    Title    = title2,
                    KeyWords = new List <Keyword>
                    {
                        CSharp,
                        CPlusPlus,
                        Windows
                    }
                },
                new Problem(body2, yh2)
                {
                    Title    = title2,
                    KeyWords = new List <Keyword>
                    {
                        CSharp,
                        CPlusPlus,
                        Windows
                    }
                },
                new Problem(body1, yh1)
                {
                    Title    = title1,
                    KeyWords = new List <Keyword>
                    {
                        CSharp,
                        CPlusPlus,
                        JAVA
                    }
                },
                new Problem(body2, yh2)
                {
                    Title    = title2,
                    KeyWords = new List <Keyword>
                    {
                        CSharp,
                        CPlusPlus,
                        Windows
                    }
                },
                new Problem(body1, yh1)
                {
                    Title    = title1,
                    KeyWords = new List <Keyword>
                    {
                        CSharp,
                        CPlusPlus,
                        JAVA
                    }
                },
                new Problem(body1, yh1)
                {
                    Title    = title1,
                    KeyWords = new List <Keyword>
                    {
                        CSharp,
                        CPlusPlus,
                        JAVA
                    }
                },
                new Problem(body2, yh2)
                {
                    Title    = title2,
                    KeyWords = new List <Keyword>
                    {
                        CSharp,
                        CPlusPlus,
                        Windows
                    }
                }
            };
            foreach (var item in problems)
            {
                ContentService.Publish(item);
            }
        }
Exemplo n.º 2
0
 static ExerciseOfLinq()
 {
     fg = new User {
         Name = "飞哥"
     };
     xy = new User {
         Name = "小余"
     };
     sql = new Keyword {
         Content = "SQL"
     };
     csharp = new Keyword {
         Content = "C#"
     };
     net = new Keyword {
         Content = ".NET"
     };
     java = new Keyword {
         Content = "JAVA"
     };
     js = new Keyword {
         Content = "JAVASCRIPT"
     };
     html = new Keyword {
         Content = "HTML"
     };
     SQL = new Article("文章")
     {
         Author = fg, Title = "SQL", Keywords = new List <Keyword> {
             sql
         }
     };
     JAVA = new Article("文章")
     {
         Author = fg, Title = "JAVA", Keywords = new List <Keyword> {
             java, html
         }
     };
     UI = new Article("文章")
     {
         Author = xy, Title = "UI", Keywords = new List <Keyword> {
             js, html, net
         }
     };
     CSharp = new Article("文章")
     {
         Author = xy, Title = "CSharp", Keywords = new List <Keyword> {
             csharp
         }
     };
     wx = new Comment(JAVA)
     {
         Body = "写的不行", Author = new User {
             Name = "王欣"
         }
     };
     atai = new Comment(SQL)
     {
         Body = "写的很好", Author = new User {
             Name = "阿泰"
         }
     };
     pzq = new Comment(UI)
     {
         Body = "还可以", Author = new User {
             Name = "彭志强"
         }
     };
     cbw = new Comment(CSharp)
     {
         Body = "一般般", Author = new User {
             Name = "陈百万"
         }
     };
     ljp = new Comment(CSharp)
     {
         Body = "看得下去", Author = new User {
             Name = "刘江平"
         }
     };
     SQL.Comments = new List <Comment> {
         atai
     };
     JAVA.Comments = new List <Comment> {
         wx
     };
     UI.Comments = new List <Comment> {
         pzq
     };
     CSharp.Comments = new List <Comment> {
         cbw, ljp
     };
     sql.Articles = new List <Article> {
         SQL
     };
     csharp.Articles = new List <Article> {
         CSharp
     };
     net.Articles = new List <Article> {
         UI
     };
     java.Articles = new List <Article> {
         JAVA
     };
     js.Articles = new List <Article> {
         UI
     };
     html.Articles = new List <Article> {
         JAVA, UI
     };
     articles = new List <Article> {
         SQL, JAVA, UI, CSharp
     };
     ContentService.Publish(UI);
     ContentService.Publish(CSharp);
     ContentService.Publish(SQL);
     ContentService.Publish(JAVA);
     ide = new Problem("求助")
     {
         Author = fg, Reward = 5, Title = "哪个ide好用"
     };
     rj = new Problem("求助")
     {
         Author = fg, Reward = 8, Title = "这个软件怎么样"
     };
     php = new Problem("求助")
     {
         Author = xy, Reward = 7, Title = "php语法"
     };
     css = new Problem("求助")
     {
         Author = xy, Reward = 6, Title = "css怎么写"
     };
     problems = new List <Problem> {
         ide, rj, php, css
     };
 }
Exemplo n.º 3
0
        public static void LinqDo()
        {
            User pzq = new User("彭志强", "555s@5")
            {
                HelpMoney = 100, HelpCradit = 100
            };
            User xlt = new User("幸龙泰", "sdoj&8")
            {
                HelpMoney = 100, HelpCradit = 100
            };
            User cy = new User("陈元", "8855*h")
            {
                HelpMoney = 100, HelpCradit = 100
            };
            User fg = new User("飞哥", "123ab!")
            {
                HelpMoney = 100, HelpCradit = 100
            };
            User xy = new User("小余", "123kk*")
            {
                HelpMoney = 100, HelpCradit = 100
            };
            Keyword sql        = new Keyword("SQL");
            Keyword js         = new Keyword("JS");
            Keyword html       = new Keyword("html");
            Keyword css        = new Keyword("css");
            Keyword csharp     = new Keyword("C#");
            Keyword dotnet     = new Keyword(".NET");
            Article LeadingEnd = new Article(fg, "前端", html, js);
            Article DataBase   = new Article(fg, "数据库", sql);
            Article AfterEnd   = new Article(fg, "后端", csharp, dotnet);
            Article UI         = new Article(xy, "UI", css);

            LeadingEnd.Publish();
            //Wait();
            DataBase.Publish();
            //Wait();
            AfterEnd.Publish();
            //Wait();
            UI.Publish();
            Comment comment1 = new Comment(pzq, "飞哥真帅", LeadingEnd);
            Comment comment2 = new Comment(cy, "讲得好", AfterEnd);
            Comment comment3 = new Comment(xlt, "沙区扛把子觉得你可以", DataBase);
            Comment comment4 = new Comment(xlt, "这个好!这个好!", UI);

            comment1.Publish();
            comment2.Publish();
            comment3.Publish();
            comment4.Publish();

            IEnumerable <Article> articles = new List <Article> {
                LeadingEnd, DataBase, AfterEnd, UI
            };


            //找出“飞哥”发布的文章
            var articleOfFg = from a in articles
                              where a.Author.Name == "飞哥"
                              select a;
            var articleOfFgLam = articles.Where(a => a.Author.Name == "飞哥");

            Console.WriteLine("飞哥发布的文章有如下:");
            foreach (var item in articleOfFg)
            {
                Console.WriteLine(item.Title);
            }
            Console.WriteLine();


            //找出2019年1月1日以后“小鱼”发布的文章
            var articleTo190101OfXy = from a in articles
                                      where a.PublishTime > new DateTime(2019 - 1 - 1) && a.Author.Name == "小余"
                                      select a;
            var articleTo190101OfXyLam = articles.Where(a => a.PublishTime > new DateTime(2019 - 1 - 1) && a.Author.Name == "小余");


            Console.WriteLine("小余于2019-01-01之后发布的文章有如下:");
            foreach (var item in articleTo190101OfXy)
            {
                Console.WriteLine(item.Title);
            }
            Console.WriteLine();
            //foreach (var item in articleTo190101OfXyLam)
            //{
            //    Console.WriteLine(item.Title);
            //}


            //按发布时间升序 / 降序排列显示文章
            var articleOrderByTimeAsce = from a in articles
                                         orderby a.PublishTime ascending
                                         select a;

            var articleOrderByTimeDesc = from a in articles
                                         orderby a.PublishTime descending
                                         select a;

            var articleOrderByTimeAsceLam = articles.OrderBy(a => a.PublishTime);
            var articleOrderByTimeDescLam = articles.OrderByDescending(a => a.PublishTime);

            Console.WriteLine("按发布时间升序排列:");
            foreach (var item in articleOrderByTimeAsce)
            {
                Console.WriteLine(item.Title);
                Console.WriteLine(item.PublishTime);
            }
            Console.WriteLine();


            //统计每个用户各发布了多少篇文章
            var articleCount = from a in articles
                               group a by a.Author into ac
                               select new
            {
                name  = ac.Key.Name,
                count = ac.Count()
            };
            var articleCountLam = articles.GroupBy(a => a.Author)
                                  .Select(ac => new
            {
                name  = ac.Key.Name,
                count = ac.Count()
            });

            Console.WriteLine("统计用户发布的文章:");
            foreach (var item in articleCount)
            {
                Console.WriteLine(item.name + item.count);
            }
            Console.WriteLine();


            //找出包含关键字“C#”或“.NET”的文章
            var articleContainToCsharpOrDotnet = from a in articles
                                                 where a.Keywords.IndexOf(csharp) >= 0 || a.Keywords.IndexOf(dotnet) >= 0
                                                 select a;
            var articleContainToCsharpOrDotnetLam = articles.Where(
                a =>
                a.Keywords.Contains(csharp) ||
                a.Keywords.Contains(dotnet));

            foreach (var item in articleContainToCsharpOrDotnet)
            {
                Console.WriteLine("包含关键字“C#”或“.NET”的文章:");
                Console.WriteLine(item.Title);
            }
            Console.WriteLine();

            //找出评论数量最多的文章,相同就取第一个
            var articleOfCommentMost = from a in articles
                                       orderby a.Comments.Count() ascending
                                       select a;

            Console.WriteLine("找出评论数量最多的文章:");
            Console.WriteLine(articleOfCommentMost.First().Title);
            Console.WriteLine();

            //找出每个作者评论数最多的文章
            var CommentMostOfAuthor = from a in articles
                                      group a by a.Author into cm
                                      select new
            {
                name  = cm.Key.Name,
                title = cm.OrderBy(c => c.Comments.Count()).First().Title
            };

            Console.WriteLine("找出每个作者评论数最多的文章:");
            foreach (var item in CommentMostOfAuthor)
            {
                Console.WriteLine(item.name + item.title);
            }
            Console.WriteLine();


            //找出每个作者最近发布的一篇文章
            var FinallyArticle = from a in articles
                                 group a by a.Author into ga
                                 select new
            {
                author = ga.Key.Name,
                title  = ga.OrderByDescending(a => a.PublishTime).First().Title
            };

            Console.WriteLine("找出每个作者最近发布的一篇文章:");
            foreach (var item in FinallyArticle)
            {
                Console.WriteLine(item.author + item.title);
            }


            Problem problem1 = new Problem("标题1", fg, 19);
            Problem problem2 = new Problem("标题2", fg);
            Problem problem3 = new Problem("标题2", xlt, 20);

            ContentService.Publish(problem1);
            ContentService.Publish(problem2);
            ContentService.Publish(problem3);
            IList <Problem> problems = new List <Problem>()
            {
                problem1, problem2, problem3
            };

            //找出每一篇求助的悬赏都大于5个帮帮币的文章作者
            var highPrice = problems.GroupBy(gp => gp.Author)
                            .Where(p => p.Min(r => r.Reward) > 5)
                            .Select(a => a.Key.Name);

            foreach (var item in highPrice)
            {
                Console.WriteLine(item);
            }
        }