我有以下的LINQ /λ表達式COMMENTS和ATTACHMENTS到RECORDS(這些是表的名稱在數據庫)相關聯。如何解決linq/lambda表達式中的組連接問題?
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在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"
}
]
}
]
NB-這是一個簡單的數據忽略字段名和值 我已經editted最後一個碼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 });
如何解決這個問題?
在背景中是否有類似於實體框架的東西?我認爲你應該嘗試使用導航屬性來代替加入。 – 2013-05-02 09:05:43
是的,有實體框架的工作。 – dany 2013-05-02 09:42:05
你可以展示班級模特嗎?我認爲使用導航屬性將使這更容易! – 2013-05-02 09:59:05