如何解决linq/lambda表达式中的组联接问题?
我有以下Linq/Lambda表达式来关联注释和附件到记录(这些是数据库中表的名称) 但对于最后一个lambda表达式,存在一个问题,即如果同一条记录有两个附件,则带有附件的记录将被复制(不是在数据库中,而是在视图中) 如图所示如何解决linq/lambda表达式中的组联接问题?,linq,sql-server-2008,asp.net-mvc-4,lambda,Linq,Sql Server 2008,Asp.net Mvc 4,Lambda,我有以下Linq/Lambda表达式来关联注释和附件到记录(这些是数据库中表的名称) 但对于最后一个lambda表达式,存在一个问题,即如果同一条记录有两个附件,则带有附件的记录将被复制(不是在数据库中,而是在视图中) 如图所示 "Data": [ { "typeid": 1, "typename": "Record Scan", "id": 3071, "title": "Late Outcomes",
"Data": [
{
"typeid": 1,
"typename": "Record Scan",
"id": 3071,
"title": "Late Outcomes",
"publishdate": "3/4/2013",
"featured": true,
"productiondate": "",
"phasename": "Board",
"updateddate": "4/29/2013",
"updateddateforsorting": "2013-04-29T19:44:29.47",
"comments": true,
"numofcomments": 4,
"attachment": true,
"numofattachments": 2,
"attachments": [
{
"attachmentid": 371,
"typeid": 1,
"id": 0,
"title": "Cardio_David.docx",
"name": null,
"createddate": "0001-01-01T00:00:00"
},
{
"attachmentid": 434,
"typeid": 1,
"id": 0,
"title": "blanks in password field.docx",
"name": null,
"createddate": "0001-01-01T00:00:00"
}
]
},
{
"typeid": 1,
"typename": "Record Scan",
"id": 3071,
"title": "Late Outcomes",
"publishdate": "3/4/2013",
"featured": true,
"productiondate": "",
"phasename": "Board",
"updateddate": "4/29/2013",
"updateddateforsorting": "2013-04-29T19:44:29.47",
"comments": true,
"numofcomments": 4,
"attachment": true,
"numofattachments": 2,
"attachments": [
{
"attachmentid": 371,
"typeid": 1,
"id": 0,
"title": "Cardio_David.docx",
"name": null,
"createddate": "0001-01-01T00:00:00"
},
{
"attachmentid": 434,
"typeid": 1,
"id": 0,
"title": "blanks in password field.docx",
"name": null,
"createddate": "0001-01-01T00:00:00"
}
]
}
]
注意-这是一个忽略字段名称和值的示例数据
我已将上一个代码recordAttachment编辑为
var recordAttachment= from rc in recordComments
join at in attachments on rc.RecordId equals at.ContentId into ra
select new { Comments = rc.Comments, CommentsCount = rc.CommentsCount Featured = rc.Featured, Id = rc.RecordId, PhaseName = rc.PhaseName, rc.ProductionDate, jac.PublishedDate, Source = jac.Source, Title = rc.Title, rc.UpdatedDate, AttachmentCount = ra.Count(), Attachments = ra, IsAttachment = (ra.Count() != null) ? true : false };
这将返回记录和相关附件。现在我需要将这些数据映射到视图模型
public class FlaggedItemModel
{
public int typeid { get; set; }
public string typename { get; set; }
public int id { get; set; }
public string title { get; set; }
public string publishdate { get; set; }
public bool featured { get; set; }
public string productiondate { get; set; }
public string phasename { get; set; }
public string updateddate { get; set; }
public DateTime updateddateforsorting { get; set; }
public bool comments { get; set; }
public int numofcomments { get; set; }
public bool attachment { get; set; }
public int numofattachments { get; set; }
public IList<AttachmentModel> attachments { get; set; }
}
如何解决此问题?出现此错误是因为您试图让数据库将日期时间转换为格式化的短日期字符串,但它不知道如何执行此操作。我建议更改您的模型,使生产日期成为datetime字段,并在视图中对其进行适当的格式化。将datetime更改为控制器(或DAL)中的字符串不是执行此操作的适当位置 或者,如果您坚持在viewmodel中使用shortdate,则首先让查询将字段作为日期时间返回,然后对查询调用.ToList(),然后将该结果投影到最终视图中,以便将结果作为datetime从数据库返回,然后让查询将其转换为C#中的shortdate。大概是这样的:
var recordlist=journalaarticleattachments.Select(x=>new{…,publishdate,…}).ToList().Select(x=>new{…,publishdate=(publishdate==null)?publishdate.ToShortDateString():string.Empty,…})代码>后台是否有类似实体框架的东西?我认为你应该尝试使用导航属性而不是加入。是的,有实体框架。你能展示一个类模型吗?我认为使用导航属性将使这更容易!你能检查一下我上面修改过的代码吗?更有趣的是看评论
,附件
和记录
。此外,您的代码在一行中很难辨认,可能您想解释“不工作”。
var recordAttachment= from rc in recordComments
join at in attachments on rc.RecordId equals at.ContentId into ra
select new { Comments = rc.Comments, CommentsCount = rc.CommentsCount Featured = rc.Featured, Id = rc.RecordId, PhaseName = rc.PhaseName, rc.ProductionDate, jac.PublishedDate, Source = jac.Source, Title = rc.Title, rc.UpdatedDate, AttachmentCount = ra.Count(), Attachments = ra, IsAttachment = (ra.Count() != null) ? true : false };
public class FlaggedItemModel
{
public int typeid { get; set; }
public string typename { get; set; }
public int id { get; set; }
public string title { get; set; }
public string publishdate { get; set; }
public bool featured { get; set; }
public string productiondate { get; set; }
public string phasename { get; set; }
public string updateddate { get; set; }
public DateTime updateddateforsorting { get; set; }
public bool comments { get; set; }
public int numofcomments { get; set; }
public bool attachment { get; set; }
public int numofattachments { get; set; }
public IList<AttachmentModel> attachments { get; set; }
}
var recordlist = journalArticleAttachments.Select(x => new FlaggedItemModel() { attachments = x.Attachments.Where(z => z.ContentId == x.Id).Select(jaa => new AttachmentModel() { attachmentid = jaa.AttachmentId, typeid = jaa.ContentType, title = jaa.FileName }).ToList(), numofcomments = x.CommentsCount, comments = x.Comments, featured = x.Featured, id = x.Id, phasename = x.PhaseName, productiondate = (x.ProductionDate.HasValue) ? x.ProductionDate.Value.ToShortDateString() : string.Empty, publishdate = (x.PublishedDate.HasValue) ? x.PublishedDate.Value.ToShortDateString() : string.Empty, title = x.Title, typeid = 1, typename = "Journal Scan", updateddate = x.UpdatedDate.ToShortDateString(), updateddateforsorting = x.UpdatedDate });