如何使用 LINQ 将结果映射到右侧属性对象
本文关键字:属性 对象 映射 何使用 LINQ 结果 | 更新日期: 2024-11-08 18:09:53
我在数据库中计算唯一值时遇到问题,我有状态为未知 = 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; }
}
你需要做一个大组,这样你才能做个人计数。 不确定除了按常量分组之外,是否有更优雅的方法可以做到这一点。
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
什么设置,因为人员表没有日期列。