2

COMMENTSATTACHMENTSRECORDSに関連付ける次の 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 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();

しかし、最後のラムダ式では、同じレコードに 2 つの添付ファイルがある場合、添付ファイルのあるレコードが重複する (データベースではなくビューで) という問題があります。

ここに示すように

 "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 });

この問題を解決するにはどうすればよいですか?

4

1 に答える 1

0

データベースに日時をフォーマットされた短い日付文字列に変換させようとしているが、その方法がわからないため、このエラーが発生しています。製造日が日時フィールドになるようにモデルを変更し、ビューで適切にフォーマットすることをお勧めします。コントローラー (または DAL) で日時を文字列に変更することは、適切な場所ではありません。

または、ビューモデルに shortdate を含めることを主張する場合は、クエリで最初にフィールドを日時として返してから、クエリで .ToList() を呼び出し、その結果を最終的なビューに投影して、結果が返されるようにします。データベースからdatetimeとして取得し、C#でshortdateに変換できます。このようなもの:

var recordlist = journalArticleAttachments.Select(x=>new { ..., publishdate, ...}).ToList().Select(x=>new { ..., publishdate = (publishdate==null) ? publishdate.ToShortDateString() : string.Empty, ...});

于 2015-03-15T04:39:39.477 に答える