簡體   English   中英

實體框架返回不同的記錄問題

[英]Entity Framework returning distinct records issue

我有一個PC Enity有一些屬性,我想基於屬性返回一個不同的對象(PC或復雜類型或其他)的列表,以便將它綁定到服務器控件,如DropDownList。 因為我的方法位於BLL我不能返回匿名類型,所以我創建了一個具有兩個peroperties的Branch ComplexType。

我寫的是這樣但它有重復的記錄:

List<Branch> result = ( from p in _context.PCs
                        where p.UserId== userId
                        select new Branch()
                                   {
                                      BranchId= p.BranchId,
                                      BranchName=p.BranchName
                                   }).Distinct().ToList();

編輯:謝謝大家,這有效:

List<PC> result = _context.PCs
                  .GroupBy(p=>p.BranchName , p.BranchId})
                  .select(g=>g.First())
                  .ToList();

這將為select語句中的所有列返回不同的行。 如果您想要特定列的不同行,請指定該特定列

List<Branch> result = ( from p in _context.PCs
                        where p.UserId== userId
                        select new Branch()
                                   {
                                      BranchId= p.BranchId,
                                    }).Distinct().ToList();

如果要基於多個列獲取不同的值,則必須創建一個組,然后從該組中選擇第一個值。 在這種情況下,您將不會使用Distinct

List<Branch> distinctResult = _context.PCs
  .GroupBy(p => new Branch {p.BranchId, p.BranchName} )
  .Select(g => g.First())
  .ToList(); 

我無法重現該問題(使用SQL Server 2008 R2和EF 4.1 / DbContext進行測試)。 您問題中的查詢...

List<Branch> result = ( from p in _context.PCs
                        where p.UserId== userId
                        select new Branch()
                        {
                            BranchId = p.BranchId,
                            BranchName = p.BranchName
                        })
                        .Distinct()
                        .ToList();

...生成以下SQL:

SELECT 
[Distinct1].[C1] AS [C1],
[Distinct1].[BranchId] AS [BranchId],
[Distinct1].[BranchName] AS [BranchName]
FROM ( SELECT DISTINCT 
       [Extent1].[BranchId] AS [BranchId], 
       [Extent1].[BranchName] AS [BranchName], 
       1 AS [C1]
       FROM [dbo].[PCs] AS [Extent1]
) AS [Distinct1]

它是兩列上的DISTINCT,我得到了預期的不同結果 - 在BranchIdBranchName沒有重復。

您得到重復項,因為Distinct()無法將兩個復雜的Branch對象識別為與其屬性相同。 它只會比較對象相等性,它將返回false(因為您創建了兩個不同的對象,但具有相同的值)。

您可以使用Distinct(IQueryable,IEqualityComparer)來提供自己的Comparer或實現IEquatable接口。

這適合我。

1。

class RolBaseComparer:IEqualityComparer<RolBase>
{
    public RolBaseComparer()
    {

    }

    public bool Equals(RolBase x, RolBase y)
    {
        if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
        {
            return false;
        }

        if (Object.ReferenceEquals(x, y))
        {
            return true;
        }

        return x.Id.Equals(y.Id) &&
                x.Nombre.Equals(y.Nombre);
    }

    public int GetHashCode(RolBase obj)
    {
        return obj.Id.GetHashCode() ^ obj.Nombre.GetHashCode();
    }
}

2。

var ResultQuery = (from ES in DbMaster.Estudiantes
                               join I in DbMaster.Inscripciones on ES.strCedula equals I.strCedEstud
                               join C in DbMaster.Carreras on I.strCodCarrera equals C.strCodigo
                               where ES.strCedula.Equals(Cedula)
                               select new RolBase { Id = "EST", Nombre = "Estudiante" }).ToList();

3。

return ResultQuery.Distinct(new RolBaseComparer()).ToList()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM