在 sql asp.net mvc 5 中使用 linq 时如何格式化记录中的数据?
Posted
技术标签:
【中文标题】在 sql asp.net mvc 5 中使用 linq 时如何格式化记录中的数据?【英文标题】:How to format your data on a record when using linq in sql asp.net mvc 5? 【发布时间】:2021-07-19 21:13:13 【问题描述】:我有一个电子表格并且有数据,但问题是我的数据是错误的,但它没有按照要求格式化,需要一些帮助。这是我对这个应用程序的逻辑。 sql 查询是针对 1 个数据库上 3 个不同表的数据库中我的记录列表,控制器和模型用于我的逻辑以根据我的要求生成以下图像。
// SQL query
SELECT TOP (1000) [WeekId]
,[WeekNum]
,[Year]
,[CreatedDate]
,[CreatedBy]
,[ModifiedDate]
,[ModifiedBy]
,[InActive]
FROM [ProductionManagement].[Schedule].[Week]
SELECT TOP (1000) [ProductionDayId]
,[WeekId]
,[ProductionDate]
,[DayOfWeek]
,[CreatedDate]
,[CreatedBy]
,[ModifiedDate]
,[ModifiedBy]
,[InActive]
FROM [ProductionManagement].[Schedule].[ProductionDay]
SELECT TOP (1000) [ModelId]
,[Name]
,[Code]
,[CreatedDate]
,[CreatedBy]
,[ModifiedDate]
,[ModifiedBy]
,[InActive]
FROM [ProductionManagement].[Schedule].[Model]
// controller
public IList<ExtractionViewModel> GetExtractionViewModels()
var db = new ProductionManagementEntities();
var scheduleList = (from p in db.ProductionDays
from m in db.Models
join w in db.Weeks on p.WeekId equals w.WeekId
orderby w.Year ascending
orderby m.Name descending
where(m.InActive == true)
select new ExtractionViewModel
Year = w.Year,
Week = w.WeekNum,
Day = p.ProductionDate,
VW250 = m.Name,
VW270 = m.Name,
VW2502PA = m.Name,
VW270PA = m.Name
).ToList();
return scheduleList;
public class ExtractionViewModel
public string Year get; set;
public int Week get; set;
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "0;yyyy-MM-dd", ApplyFormatInEditMode =false)]
public DateTime Day get; set;
public string VW250 get; set;
public string VW270 get; set;
public string VW2502PA get; set;
public string VW270PA get; set;
【问题讨论】:
@mjwills,要求年份必须是今年,如果有一周可用,则周必须显示 9,如果今年/日有记录,则型号必须为 79。 @mjwills 输出我已将其附加到我的最后一个代码中,作为图像。这是我作为 excel 的输出。 不同的是我的输出和它应该做的不一样,我必须产生的输出必须和要求一样,希望我有意义。这就是我现在卡住的地方。 我不明白你的意思。祝你一切顺利。 为什么 m.Name 对于所有值都相同:VW250 = m.Name, VW270 = m.Name, VW2502PA = m.Name, VW270PA = m.Name 【参考方案1】:尝试以下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication
public class Program
public static void Main(string[] args)
var db = new ProductionManagementEntities();
var scheduleList = (from p in db.ProductionDays.OrderBy(x => x.CreatedDate)
join w in db.Weeks on p.WeekId equals w.WeekId
join m in db.Models.Where(x =>x.InActive == false) on p.CreatedDate equals m.CreatedDate
select new p = p, w = w, m = m
).GroupBy(x => x.p.CreatedDate)
.Select(x => new ExtractionViewModel()
Year = x.Key.Year,
Week = x.FirstOrDefault().w.WeekId,
Day = x.FirstOrDefault().p.ProductionDate,
VW250 = x.Where(y => y.m.Name == "VM250").Count(),
VW270 = x.Where(y => y.m.Name == "VM270").Count(),
VW2502PA = x.Where(y => y.m.Name == "VM2502PA").Count(),
VW270PA = x.Where(y => y.m.Name == "VM270PA").Count()
).ToList();
public class ProductionManagementEntities
public List<Week> Weeks get;set;
public List<ProductionDay> ProductionDays get;set;
public List<Model> Models get;set;
public class Week
public int WeekId get;set;
public DateTime CreatedDate get;set;
public string CreatedBy get;set;
public DateTime ModifiedDate get;set;
public string ModifiedBy get;set;
public Boolean InActive get;set;
public class ProductionDay
public int WeekId get;set;
public DateTime ProductionDate get;set;
public DayOfWeek DayOfWeek get;set;
public DateTime CreatedDate get;set;
public string CreatedBy get;set;
public DateTime ModifiedDate get;set;
public string ModifiedBy get;set;
public Boolean InActive get;set;
public class Model
public string Name get; set;
public int Code get; set;
public DateTime CreatedDate get; set;
public string CreatedBy get; set;
public DateTime ModifiedDate get; set;
public string ModifiedBy get; set;
public Boolean InActive get; set;
public class ExtractionViewModel
public int Year get; set;
public int Week get; set;
public DateTime Day get; set;
public int VW250 get; set;
public int VW270 get; set;
public int VW2502PA get; set;
public int VW270PA get; set;
【讨论】:
以上是关于在 sql asp.net mvc 5 中使用 linq 时如何格式化记录中的数据?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Asp.Net Core Mvc 5.0 中将 sql 数据库与 ado.net 连接?