简体   繁体   中英

Distinct not working in entity framework

function for get list of documents name only with distinct...

public static List<DocumentTypeModel> GetUploadedDocumentsName(int TicketId)
{
    List<DocumentTypeModel> documents = new List<DocumentTypeModel>();
    using (var db = new UnitOfWork())
    {
        documents = db.tbl_TrnTicketDocument.Get(x => x.FK_TicketId == TicketId && x.IsActive == true).Select(s => new DocumentTypeModel()
        {
            DocumentTypeNameEnglish = s.tbl_MstDocumentType.DocumentTypeNameEnglish
        }).Distinct().ToList();
    }
    return documents;
}

currently result is this -

Affidavit in Case of Cancelled Will/No Will

Affidavit in Case of Cancelled Will/No Will

Allotment Letter

Allotment Letter

Death Certificate

Death Certificate

Lease Deed

Lease Deed

Photo Identity of Applicant

Photo Identity of Applicant

Possession Letter

Possession Letter

Registered/Unregistered Will

Registered/Unregistered Will

You can use groupby and select first option like this:

List<DocumentTypeModel> documents = new List<DocumentTypeModel>();
using (var db = new UnitOfWork())
{
   documents = db.tbl_TrnTicketDocument.Get(x => x.FK_TicketId == TicketId && x.IsActive == true).Select(s => new DocumentTypeModel()
   {
   DocumentTypeNameEnglish = s.tbl_MstDocumentType.DocumentTypeNameEnglish
   }).ToList();

   documents = documents.GroupBy(x => x.DocumentTypeNameEnglish).Select(g => g.First());
}

Distinct() doesn't work like you tried on objects. Use IComparer to get this working https://support.microsoft.com/en-us/kb/320727

Create a comparer class

public class DocumentTypeModelComparer: IComparer
{
  int IComparer.Compare(object a, object b)
 {
   if(a.Id == b.ID)
     return 0;
   else
     return 1;
  }
}

Now in your lambda expression

 documents = db.tbl_TrnTicketDocument.Get(x => x.FK_TicketId == TicketId && x.IsActive == true).Select(s => new DocumentTypeModel()
        {
            DocumentTypeNameEnglish = s.tbl_MstDocumentType.DocumentTypeNameEnglish
        }).ToList().Distinct(new DocumentTypeModelComparer()).ToList();

You are selecting the documenttypemodel and this is the distinct part (so all fields are checked), you probably want the distinct by via https://github.com/morelinq/MoreLINQ , or you can use group by with first see( linq distinct or group by multiple properties ).
The other option is to select only the DocumentTypeNameEnglish field and you will get the unique documents.

 documents = db.tbl_TrnTicketDocument.Get(x => x.FK_TicketId == TicketId && x.IsActive == true).Select(s => new  { documentTypes =s.tbl_MstDocumentType.DocumentTypeNameEnglish}).Distinct().ToList();

Hopefully this is what you want, if not can you post more details?

根据弗拉基米尔的评论切换到下面:

 .ToList().Distinct();

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