LINQ怎么写新用户的发帖?

刘小安 2016-09-22 11:02:28
假设我有个表,字段是username createtime

我怎么查询今天发帖的用户,哪些是第一次发帖?哪些不是第一次发帖?
用LINQ怎么写呢?我有点搞懵了。

求教!

我现在是分开写的,先把今天发帖的用户查出来,然后再查用户历史是否发帖,再来判断。但是这样就很慢了。因为我是查的数据库。有没有办法一句linq写出来呢?

麻烦了!
...全文
114 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
刘小安 2016-09-22
  • 打赏
  • 举报
回复
db.EcOrders.Where(o => o.CreateTime >= tempStart && o.CreateTime < tempEnd && db.EcOrders.Where(x => x.CreateTime >= tempStart && x.CreateTime < tempEnd) .Select(x => x.UserId) .Except(db.EcOrders.Where(x => x.CreateTime < tempStart).Select(x => x.UserId)) .Contains(o.UserId) ).Count();
刘小安 2016-09-22
  • 打赏
  • 举报
回复
搞定了,我没这样写。我写的linq里面再嵌套linq。我不是用的from o in 这种写法。谢谢
正怒月神 版主 2016-09-22
  • 打赏
  • 举报
回复
static void Main(string[] args)
        {
            //测试数据
            List<UserMsg> ml = new List<UserMsg>() { 
                new UserMsg(){username="1",createtime=DateTime.Now.AddDays(-1).Date},
                 new UserMsg(){username="1",createtime=DateTime.Now.Date},
                  new UserMsg(){username="1",createtime=DateTime.Now.Date},
                   new UserMsg(){username="2",createtime=DateTime.Now.AddDays(-1).Date},
                   new UserMsg(){username="2",createtime=DateTime.Now.Date},
                   new UserMsg(){username="2",createtime=DateTime.Now.Date},
                   new UserMsg(){username="3",createtime=DateTime.Now.Date},
                   new UserMsg(){username="4",createtime=DateTime.Now.AddDays(-1).Date},
            };

            var q = from m in ml
                    where m.createtime == DateTime.Now.Date
                    group m by m.username into g1
                    select new
                    {
                        name = g1.Key,
                        isFirst=ml.Count(x=>x.username==g1.Key)>1?false:true,
                    };

            foreach (var item in q)
            {
                Console.WriteLine(item.name+"---"+item.isFirst);
            }

            Console.ReadLine();

        }

        public class UserMsg
        {
            public string username { get; set; }
            public DateTime createtime { get; set; }
        }

62,073

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧