简体   繁体   中英

generalize Select method of LINQ to Entities in EF5

I have many cross link tables with two link columns linking to parent tables such as:

CREATE TABLE [dbo].[InsuranceDocuments]
(
    [InsuranceDocumentId] [bigint] IDENTITY(1,1) NOT NULL,
    [InsuranceId] [bigint] NOT NULL,
    [DocumentId] [bigint] NOT NULL
)

CREATE TABLE [dbo].[PersonDocuments]
(
    [PersonDocumentId] [bigint] IDENTITY(1,1) NOT NULL,
    [PersonId] [bigint] NOT NULL,
    [DocumentId] [bigint] NOT NULL
)

1) InsuranceId links to Insurances table

2) PersonId links to Persons table

3) DocumentId links to Documents table

On screens like "Person Details" or "Insurance Details" only document information is needed to show on initial load, so my "Documents List" structure is the same for all cases like that. Documents table definition I will omit (too big) but it has basic stuff like Path, DocumentCategoryId etc.

Here is my LINQ for Person and Insurance documents:

 public IEnumerable<Models.SearchResult.Document> GetInsuranceDocuments(long parentId)
        {

            using (var db = Core.GetDb())
            {

                var items = db.InsuranceDocuments.Where(a => a.InsuranceId == parentId)
                    .Select(a => new Models.SearchResult.Document
                    {
                        CategoryId = a.Document.DocumentCategoryId,
                        TypeId = a.Document.DocumentTypeId,
                        CreateDate = a.CreateDate,
                        Name = (a.Document.DocumentName == null ? a.Document.DocumentPath : a.Document.DocumentName)

                    }).OrderBy(a=>a.Name).ToArray();
                return items;
            }

        }

 public IEnumerable<Models.SearchResult.Document> GetPersonDocuments(long parentId)
        {

            using (var db = Core.GetDb())
            {

                var items = db.PersonDocuments.Where(a => a.PersonId == parentId)
                    .Select(a => new Models.SearchResult.Document
                    {
                        CategoryId = a.Document.DocumentCategoryId,
                        TypeId = a.Document.DocumentTypeId,
                        CreateDate = a.CreateDate,
                        Name = (a.Document.DocumentName == null ? a.Document.DocumentPath : a.Document.DocumentName)

                    }).OrderBy(a=>a.Name).ToArray();
                return items;
            }
    }

as you can see Select method is the same for both
and would be nice to generalize that part, need help with that. Thank you.

extract method and use it

public Models.SearchResult.Document[] GetDocumentsArray(IQueryable<Models.SearchResult.Document> docs)
{
     return docs.Select(a => new Models.SearchResult.Document
                    {
                        CategoryId = a.Document.DocumentCategoryId,
                        TypeId = a.Document.DocumentTypeId,
                        CreateDate = a.CreateDate,
                        Name = (a.Document.DocumentName == null ? a.Document.DocumentPath : a.Document.DocumentName)

                    }).OrderBy(a=>a.Name).ToArray();
}

using

var items = GetDocumentsArray(db.PersonDocuments.Where(a => a.PersonId == parentId));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM