改变从数据库返回的值的外观(DOT NET)

huangapple go评论61阅读模式
英文:

Change Appearance of the values that returned form database (DOT NET)

问题

我想要改变从数据库返回的值的外观,就像我想要的那样。修改值后,我想将这个外观分配给视图模型。

例如,我有一个模型,其中包含以下字段:
ID,用户名,历史日期,时钟。
视图模式
ID,用户名,历史日期,签到,签退

如果时钟索引 % 2!= 0(偶数),我想要按用户名和历史日期分组,并将该值插入到签到中
否则,如果索引 % 2 == 0(偶数),将该值插入到签退中

有关更多详细信息-->
我尝试了这个

var Filter = _context.Attendances
             .GroupBy(x => new { x.Username, x.HistorDay }) 
             .Select(x => new {
                Username = x.Key.Username, 
                HistorDay = x.Key.HistorDay,
                In = x.Select(c => c.Clock).Where((q, w) => w % 2 != 0),
                Out = x.Select(c => c.Clock).Where((q, w) => w % 2 == 0),
             });

但是得到了异常System.Collections.Generic.IEnumerable`1[Eams.Models.Attendance]。

我尝试了Svyatoslav Danyliv的这段代码

var rawData = _context.Attandances
              .Select(a => new { a.Username, a.HistorDay, a.Clock })
              .ToList();

var result = rawData
            .GroupBy(x => new { x.Username, x.HistorDay })
            .Select(x => new 
            {
                Username = x.Key.Username,
                HistorDay = x.Key.HistorDay,
                In = x.Select(c => c.Clock).Where((q, w) => w % 2 != 0).ToList(),
                Out = x.Select(c => c.Clock).Where((q, w) => w % 2 == 0).ToList(),
            });

foreach (var item in result)
{
    Debug.WriteLine($"ALL {item}");
}

在控制台中--> 结果 ==>

ALL { Username = 43091, HistorDay = 6/23/2023 12:00:00 AM, In = System.Collections.Generic.List1[System.TimeSpan], Out = System.Collections.Generic.List1[System.TimeSpan] }

英文:

I would like to change the appearance of the values that are returned from the databases as I wish. After modifying the values, I would like to assign this appearance to View Model

e.g. I have a Model that contains these field
ID, Username,HistorDay,Clock.
View Mode
ID,Username,HistorDay, check-in, check-out

I want to group by Username and HistorDay if Clock index % 2! = 0 (even) insert this value to check-in
else index % 2 == 0 (even) insert this value to check-out

For More Details -->
I try this

var Filter = _context.Attendances
             .GroupBy(x => new { x.Username, x.HistorDay }) 
             .Select(x => new {
           Username = x.Key.Username, 
           HistorDay = x.Key.HistorDay,
           In = x.Select(c => c.Clock).Where((q, w) => w % 2 != 0),
           Out = x.Select(c => c.Clock).Where((q, w) => w % 2 == 0),
}); 

but get Exception System.Collections.Generic.IEnumerable`1[Eams.Models.Attendance]'

I try this code form Svyatoslav Danyliv

var rawData = _context.Attandances
              .Select(a => new { a.Username, a.HistorDay, a.Clock })
              .ToList();

var result = rawData
            .GroupBy(x => new { x.Username, x.HistorDay })
            .Select(x => new 
            {
                Username = x.Key.Username,
                HistorDay = x.Key.HistorDay,
                In = x.Select(c => c.Clock).Where((q, w) => w % 2 != 0).ToList(),
                Out = x.Select(c => c.Clock).Where((q, w) => w % 2 == 0).ToList(),
            });

        foreach (var item in result)
        {
            Debug.WriteLine($"ALL {item}");
        }

in console --> Result ==>

ALL { Username = 43091, HistorDay = 6/23/2023 12:00:00 AM, In = System.Collections.Generic.List1[System.TimeSpan], Out = System.Collections.Generic.List1[System.TimeSpan] }

答案1

得分: 0

"Value Conversions"可以在使用"EF Core"时使用。这是最佳方法。以下是示例和如何使用它的方式:
https://learn.microsoft.com/en-us/ef/core/modeling/value-conversions?tabs=data-annotations

英文:

"Value Conversions" can be used if you are using "EF Core". That's the best way to do it. Here's an example and how to use it
https://learn.microsoft.com/en-us/ef/core/modeling/value-conversions?tabs=data-annotations

答案2

得分: 0

为了处理您的查询,需要从表单数据库中选择最小数据,然后在客户端端进行后处理:

var rawData = await _context.Attendances
  .Select(a => new { a.Username, a.HistorDay, a.Clock })
  .ToListAsync();

var result = rawData
    .GroupBy(x => new { x.Username, x.HistorDay }) 
    .Select(x => new {
        Username = x.Key.Username, 
        HistorDay = x.Key.HistorDay,
        In = x.Select(c => c.Clock).Where((q, w) => w % 2 != 0).ToList(),
        Out = x.Select(c => c.Clock).Where((q, w) => w % 2 == 0).ToList(),
});

这是代码的翻译部分,不包含其他内容。

英文:

For your query it is needed to select minimal data from form database and then post process on the client side:

var rawData = await _context.Attendances
  .Select(a => new { a.Username, a.HistorDay, a.Clock })
  .ToListAsync();

var result = rawData
    .GroupBy(x => new { x.Username, x.HistorDay }) 
    .Select(x => new {
        Username = x.Key.Username, 
        HistorDay = x.Key.HistorDay,
        In = x.Select(c => c.Clock).Where((q, w) => w % 2 != 0).ToList(),
        Out = x.Select(c => c.Clock).Where((q, w) => w % 2 == 0).ToList(),
}); 

huangapple
  • 本文由 发表于 2023年6月22日 13:49:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528906.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定