我有以下 Linq/Lambda 表达式将COMMENTS和ATTACHMENTS关联到RECORDS(这些是数据库中表的名称)。
var comments = repositoryBase.Query<Comments>()
.Select(x => x);
var attachments = repositoryBase.Query<Attachment>()
.Where(x => x.DeletedFlag == false)
.Select(x => x);
var recordItems = repositoryBase.Query<Record>()
.Where(x => x.DepartmentId == departmentIdId && x.DeletedFlag == false);
recordItems = recordItems
.Where(x => x.Featured == true && (x.PhaseTypeId == 2 || x.PhaseTypeId == 3)); // filtering
var recordComments = recordItems
.GroupJoin(comments,
itm => new { a = 1, b = itm.RecordId },
c => new { a = c.TypeId, b = c.Id },
(itm, c) => new { c, itm })
.SelectMany(x => x.c.DefaultIfEmpty(), (x, y) => new
{
Comments = (y != null) ? true : false,
CommentsCount = x.c.Count(),
RecordId = x.itm.RecordId,
Featured = x.itm.Featured,
Id = x.itm.RecordId,
PhaseName = x.itm.PhaseType.PhaseName,
x.itm.ProductionDate,
x.itm.PublishedDate,
Title = x.itm.RecordTitle,
x.itm.UpdatedDate
}).Distinct();
其中 TypeId 和 Id in是完成组加入(左外加入)的评论c => new { a = c.TypeId, b = c.Id }
中的字段。
var recordAttachments = recordComments
.GroupJoin(attachments,
itm => new { a = 1, b = itm.RecordId },
at => new { a = at.ContentType, b = at.ContentId },
(itm, at) => new { at, itm})
.SelectMany(x => x.at.DefaultIfEmpty(), (x, y) => new
{
Attachments = (y != null) ? true : false,
AttachmentsCount = x.at.Count(),
AttachmentTitle = y.FileName,
AttachmentId = (y != null) ? y.AttachmentId : 0,
TypeId = (y != null) ? y.ContentType : 0,
ItemId = (y != null) ? y.ContentId : 0,
Comments = x.itm.Comments,
CommentsCount = x.itm.CommentsCount,
Featured = x.itm.Featured,
Id = x.itm.RecordId,
PhaseName = x.itm.PhaseName,
x.itm.ProductionDate,
x.itm.PublishedDate,
Title = x.itm.Title,
x.itm.UpdatedDate
}).Distinct().ToList();
但是对于最后一个 lambda 表达式,存在一个问题,即如果同一记录有两个附件,则带有附件的记录会重复(不在数据库中,而是在视图中)。
如此处所示
"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; }
}
我试过这段代码但没有工作
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 });
如何解决这个问题?