LINQ如何实现考勤查询

荷塘散人 2017-04-26 10:27:15
要实现这么个功能,不知道怎么写查询语句
查询考勤数据,每个人每天的考勤数据可能有多条,考勤统计查询时,需要展现某个时间段内(比如一个月)所有人的考勤时间,
表名:AttendanceInfo
字段:userName用户名
userCard员工编号
ClockTime 打卡时间

用Linq 查出某个时间段内,所有人每天的考勤时间,即每天的最早打卡时间和最晚打卡时间。
...全文
162 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
by_封爱 2017-04-26
  • 打赏
  • 举报
回复

var q=list.where(时间条件).
groupby(new {用户id,日期(yyyy-mm-dd)}).
select(new {uid=用户id,日期=日期,最早=list.firstordefault(用户id=id&&日期=日期,最晚=list.lastordefault(用户id=id&&日期=日期)});
伪代码 大致如上...自己整理下 应该是可以实现的
荷塘散人 2017-04-26
  • 打赏
  • 举报
回复
引用 1 楼 hht006158 的回复:
查询出某个时间段内,所有人每天的考勤时间 var listinfo=AttendanceInfo.Where(d => d.ClockTime > "时间" && d.ClockTime <"时间"); 每天的最早打卡时间和最晚打卡时间 相同记录的做比较取出 最大 最小数据
这是查出某段时间里的所有打卡时间,而不是我说的只有每天的最早最晚时间;用代码怎么实现
八零末愤青 2017-04-26
  • 打赏
  • 举报
回复
查询出某个时间段内,所有人每天的考勤时间 var listinfo=AttendanceInfo.Where(d => d.ClockTime > "时间" && d.ClockTime <"时间"); 每天的最早打卡时间和最晚打卡时间 相同记录的做比较取出 最大 最小数据
正怒月神 2017-04-26
  • 打赏
  • 举报
回复
引用 7 楼 yszx811 的回复:
报错:LINQ to Entities 不支持指定的类型成员“Date”。只支持初始值设定项、实体成员和实体导航属性。
你的可能是datetime? 类型吧?
public class User
    {

        public virtual int id { get; set; }
        public virtual string name { get; set; }
        public virtual DateTime cardTime { get; set; }

    }
荷塘散人 2017-04-26
  • 打赏
  • 举报
回复
引用 4 楼 hanjun0612 的回复:
static List<User> list = new List<User>()
        {
            new User(){id=1,name="Jay",cardTime=DateTime.Parse("2017-04-26 9:00:00")},
            new User(){id=1,name="Jay",cardTime=DateTime.Parse("2017-04-26 10:00:00")},
            new User(){id=1,name="Jay",cardTime=DateTime.Parse("2017-04-26 15:00:00")},
            new User(){id=1,name="Jay",cardTime=DateTime.Parse("2017-04-27 8:00:00")},
            new User(){id=1,name="Jay",cardTime=DateTime.Parse("2017-04-27 9:00:00")},
            new User(){id=1,name="Jay",cardTime=DateTime.Parse("2017-04-27 12:00:00")},

            new User(){id=2,name="h12",cardTime=DateTime.Parse("2017-04-26 9:00:00")},
            new User(){id=2,name="h12",cardTime=DateTime.Parse("2017-04-26 22:00:00")},
            new User(){id=2,name="h12",cardTime=DateTime.Parse("2017-04-26 23:00:00")},
            new User(){id=2,name="h12",cardTime=DateTime.Parse("2017-04-27 9:00:00")},
            new User(){id=2,name="h12",cardTime=DateTime.Parse("2017-04-28 9:00:00")},
            new User(){id=2,name="h12",cardTime=DateTime.Parse("2017-04-28 10:00:00")},
            new User(){id=2,name="h12",cardTime=DateTime.Parse("2017-04-28 11:00:00")},
            new User(){id=2,name="h12",cardTime=DateTime.Parse("2017-04-29 9:00:00")},
            new User(){id=2,name="h12",cardTime=DateTime.Parse("2017-04-29 21:00:00")},
        };


        static void Main(string[] args)
        {
            //每人,每天最早最晚的记录
            var o = list.GroupBy(x => new { x.id, x.cardTime.Date }).Select(g => new { g, count = g.Count() })  
                .SelectMany(t => t.g.Select((b, i) => new { b, i,t.count })).Where(x=>x.i==0 || x.i==(x.count-1));

            foreach (var item in o)
            {
                Console.WriteLine("{0}  {1}  {2}", item.b.id, item.b.name, item.b.cardTime);
            }
            
            Console.ReadLine();
        }
报错:LINQ to Entities 不支持指定的类型成员“Date”。只支持初始值设定项、实体成员和实体导航属性。
by_封爱 2017-04-26
  • 打赏
  • 举报
回复
你代码写错了而已. 借用月神的list. 如下代码.

var q = users.GroupBy(d => new { d.name, d.cardTime.Date }).
                Select(d => new
                {
                    名字 = d.Key.name,
                    日期 = d.Key.Date.ToString("yyyy-MM-dd"),
                    最早 = users.FirstOrDefault(x => x.name == d.Key.name && x.cardTime.Date == d.Key.Date.Date).cardTime,
                    最晚 = users.LastOrDefault(x => x.name == d.Key.name && x.cardTime.Date == d.Key.Date.Date).cardTime
                }).ToList();
            q.ForEach(item => {
                Console.WriteLine(item.ToJson());
            });
得到

{"名字":"Jay","日期":"2017-04-26","最早":"2017-04-26T09:00:00","最晚":"2017-04-26T15:00:00"}
{"名字":"Jay","日期":"2017-04-27","最早":"2017-04-27T08:00:00","最晚":"2017-04-27T12:00:00"}
{"名字":"h12","日期":"2017-04-26","最早":"2017-04-26T09:00:00","最晚":"2017-04-26T23:00:00"}
{"名字":"h12","日期":"2017-04-27","最早":"2017-04-27T09:00:00","最晚":"2017-04-27T09:00:00"}
{"名字":"h12","日期":"2017-04-28","最早":"2017-04-28T09:00:00","最晚":"2017-04-28T11:00:00"}
{"名字":"h12","日期":"2017-04-29","最早":"2017-04-29T09:00:00","最晚":"2017-04-29T21:00:00"}
荷塘散人 2017-04-26
  • 打赏
  • 举报
回复
引用 3 楼 diaodiaop 的回复:

var q=list.where(时间条件).
groupby(new {用户id,日期(yyyy-mm-dd)}).
select(new {uid=用户id,日期=日期,最早=list.firstordefault(用户id=id&&日期=日期,最晚=list.lastordefault(用户id=id&&日期=日期)});
伪代码 大致如上...自己整理下 应该是可以实现的
用你的方法可以取到最早最迟的时间,但结果是这样的: 如果某人某天考勤记录有5条,那么通过你的语句,得到的5条数据都一样的,都为姓名+某天最早时间+某天最迟时间,怎么把这5条重复的编程一条数据呢
正怒月神 2017-04-26
  • 打赏
  • 举报
回复
static List<User> list = new List<User>()
{
new User(){id=1,name="Jay",cardTime=DateTime.Parse("2017-04-26 9:00:00")},
new User(){id=1,name="Jay",cardTime=DateTime.Parse("2017-04-26 10:00:00")},
new User(){id=1,name="Jay",cardTime=DateTime.Parse("2017-04-26 15:00:00")},
new User(){id=1,name="Jay",cardTime=DateTime.Parse("2017-04-27 8:00:00")},
new User(){id=1,name="Jay",cardTime=DateTime.Parse("2017-04-27 9:00:00")},
new User(){id=1,name="Jay",cardTime=DateTime.Parse("2017-04-27 12:00:00")},

new User(){id=2,name="h12",cardTime=DateTime.Parse("2017-04-26 9:00:00")},
new User(){id=2,name="h12",cardTime=DateTime.Parse("2017-04-26 22:00:00")},
new User(){id=2,name="h12",cardTime=DateTime.Parse("2017-04-26 23:00:00")},
new User(){id=2,name="h12",cardTime=DateTime.Parse("2017-04-27 9:00:00")},
new User(){id=2,name="h12",cardTime=DateTime.Parse("2017-04-28 9:00:00")},
new User(){id=2,name="h12",cardTime=DateTime.Parse("2017-04-28 10:00:00")},
new User(){id=2,name="h12",cardTime=DateTime.Parse("2017-04-28 11:00:00")},
new User(){id=2,name="h12",cardTime=DateTime.Parse("2017-04-29 9:00:00")},
new User(){id=2,name="h12",cardTime=DateTime.Parse("2017-04-29 21:00:00")},
};


static void Main(string[] args)
{
//每人,每天最早最晚的记录
var o = list.GroupBy(x => new { x.id, x.cardTime.Date }).Select(g => new { g, count = g.Count() })
.SelectMany(t => t.g.Select((b, i) => new { b, i,t.count })).Where(x=>x.i==0 || x.i==(x.count-1));

foreach (var item in o)
{
Console.WriteLine("{0} {1} {2}", item.b.id, item.b.name, item.b.cardTime);
}

Console.ReadLine();
}

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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