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

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

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

问题

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

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

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

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

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

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

我尝试了Svyatoslav Danyliv的这段代码

  1. var rawData = _context.Attandances
  2. .Select(a => new { a.Username, a.HistorDay, a.Clock })
  3. .ToList();
  4. var result = rawData
  5. .GroupBy(x => new { x.Username, x.HistorDay })
  6. .Select(x => new
  7. {
  8. Username = x.Key.Username,
  9. HistorDay = x.Key.HistorDay,
  10. In = x.Select(c => c.Clock).Where((q, w) => w % 2 != 0).ToList(),
  11. Out = x.Select(c => c.Clock).Where((q, w) => w % 2 == 0).ToList(),
  12. });
  13. foreach (var item in result)
  14. {
  15. Debug.WriteLine($"ALL {item}");
  16. }

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

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

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

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

I try this code form Svyatoslav Danyliv

  1. var rawData = _context.Attandances
  2. .Select(a => new { a.Username, a.HistorDay, a.Clock })
  3. .ToList();
  4. var result = rawData
  5. .GroupBy(x => new { x.Username, x.HistorDay })
  6. .Select(x => new
  7. {
  8. Username = x.Key.Username,
  9. HistorDay = x.Key.HistorDay,
  10. In = x.Select(c => c.Clock).Where((q, w) => w % 2 != 0).ToList(),
  11. Out = x.Select(c => c.Clock).Where((q, w) => w % 2 == 0).ToList(),
  12. });
  13. foreach (var item in result)
  14. {
  15. Debug.WriteLine($"ALL {item}");
  16. }

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

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

  1. var rawData = await _context.Attendances
  2. .Select(a => new { a.Username, a.HistorDay, a.Clock })
  3. .ToListAsync();
  4. var result = rawData
  5. .GroupBy(x => new { x.Username, x.HistorDay })
  6. .Select(x => new {
  7. Username = x.Key.Username,
  8. HistorDay = x.Key.HistorDay,
  9. In = x.Select(c => c.Clock).Where((q, w) => w % 2 != 0).ToList(),
  10. Out = x.Select(c => c.Clock).Where((q, w) => w % 2 == 0).ToList(),
  11. });

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

英文:

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

  1. var rawData = await _context.Attendances
  2. .Select(a => new { a.Username, a.HistorDay, a.Clock })
  3. .ToListAsync();
  4. var result = rawData
  5. .GroupBy(x => new { x.Username, x.HistorDay })
  6. .Select(x => new {
  7. Username = x.Key.Username,
  8. HistorDay = x.Key.HistorDay,
  9. In = x.Select(c => c.Clock).Where((q, w) => w % 2 != 0).ToList(),
  10. Out = x.Select(c => c.Clock).Where((q, w) => w % 2 == 0).ToList(),
  11. });

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:

确定