我在计算数据库中的唯一值时遇到问题我有用户表状态为Unknown = 0,In = 1,Out = 2,UnknownWasIn = 3,UnknownWasOut = 4,问题是我不知道因为我们对它进行了硬编码,所以在数据库中有这个值的查找表。我创建了课程StatusCounter
using System;
using System.Dynamic;
public class StatusCounter
{
public int Total { get; set; }
public int In { get; set; }
public int Out { get;set ; }
public int Unknow { get; set; }
public DateTime LastUpdatedDateTime { get; set; }
}
我想问一下如何使用linq
将值映射到正确的属性var result = from people in TBL_People
where people.Deleted == false
group people by people.Status into s
select new StatusCounter
TBLPeople
public partial class TBLPeople
{
public int PersonID { get; set; }
public Nullable<int> Status { get; set; }
public string Email { get; set; }
}
答案 0 :(得分:1)
你需要做一个大团体,这样你就可以做个人计数。不确定是否有更优雅的方法来做到这一点,而不仅仅是按常量分组。
var result =
from people in TBL_People
where people.Deleted == false
group people by 1 into s
select new StatusCounter
{
Total = s.Count(),
In = s.Count(p => p.Status == 1),
Out = s.Count(p => p.Status == 2),
Unknown = s.Count(p => p.Status == 3 || p.Status == 4),
LastUpdatedDateTime = DateTime.Now
}
由于人员表没有日期列,我不确定您要将LastUpdatedDateTime
设置为什么。