6

我正在尝试做一个不包含重复IdUser值的查询,但不起作用。

这是我的 linq 查询:

var sql= (from u in db.USER
          join c in db.CONSULT on u.IdUser equals c.IdUser 
          select new UsuersViewModel 
                 {  
                    IdUser = c.IdUser, 
                    DateCreate=c.DateCreate, 
                    IdTypeConsult = c.IdTypeConsult, 
                    Sex=u.Sex 
                 })
                 .Distinct();

我要这个:

SELECT   distinct CONSULT.IdUser , CONSULT.DateCreate, 
         CONSULT.IdTypeConsult , USER.Sex
FROM   CONSULT INNER JOIN
       USER ON CONSULT.IdUser = USER.IdUser 

查询给出重复的记录

为什么它不起作用?

4

3 回答 3

7

我认为您想使用Distinct(IEqualityComparer<T>)重载。你需要创建一个IEqualityComparer来做你想做的事:

class UserComparer : IEqualityComparer<UsuersViewModel >
{
    public bool Equals(UsuersViewModel  x, UsuersViewModel y)
    {
        //Check whether the compared objects reference the same data.
        if (Object.ReferenceEquals(x, y)) return true;

        //Check whether any of the compared objects is null.
        if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
            return false;

        return x.IdUser == y.IdUser;
    }

    // If Equals() returns true for a pair of objects 
    // then GetHashCode() must return the same value for these objects.

    public int GetHashCode(UsuersViewModel  user)
    {
        //Check whether the object is null
        if (Object.ReferenceEquals(user, null)) return 0;

        return user.IdUser == null ? 0 : user.IdUser.GetHashCode();
    }
}

然后像这样使用它:

var comparer = new UserComparer();
var sql= (from u in db.USER
          join c in db.CONSULT on u.IdUser equals c.IdUser 
          select new UsuersViewModel 
                 {  
                    IdUser = c.IdUser, 
                    DateCreate=c.DateCreate, 
                    IdTypeConsult = c.IdTypeConsult, 
                    Sex=u.Sex 
                 })
                 .Distinct(comparer);

我不确定这是否会生成您想要的 SQL,但可能会得到您想要的结果。

于 2012-07-26T16:27:00.287 回答
2

在比较类实例(与匿名类型)时,您需要定义“平等”。对于匿名类型,编译器假定相等意味着“所有字段都相等”,就像 SQL 一样。所以你有几个选择:

  1. 在查询中使用匿名类型,使用.Distinct(),然后转换为强类型,
  2. 定义一个IEqualityComparer<Usuers>类并将其传递给Distinct,
  3. 覆盖Equals(和GetHashCodeUsuers

2) 和 3) 将是非常相似的代码。2)更灵活(您可以通过定义不同的类以不同的方式定义相等,而 3)将在您比较Uusers实例时使用(不仅在此查询中)。

在此处查看我对类似问题的回答。

于 2012-07-26T16:24:46.127 回答
0

注意:以下可以在 LinqPad 中运行(在http://www.linqpad.net/免费) - 只需将 Language 下拉菜单设置为“C# Program”并将代码粘贴到编辑器窗口中。

您可以使用“组”来提供您的独特要求,如下所示:

void Main()
{
    var db = new DataBase();
    var sql= (from u in db.USER
          join c in db.CONSULT on u.IdUser equals c.IdUser 
          group c by new { c.IdUser, c.DateCreate, c.IdTypeConsult, u.Sex } into gc
          select new UsuersViewModel 
                 {  
                    IdUser = gc.Key.IdUser, 
                    DateCreate=gc.Key.DateCreate, 
                    IdTypeConsult = gc.Key.IdTypeConsult, 
                    Sex=gc.Key.Sex 
                 })
                 .Distinct();
    sql.Dump("SQL Distinct Demo");
}
public class Consultation {
    public int  IdUser {get;set;}
    public DateTime DateCreate {get;set;}
    public int IdTypeConsult {get;set;}
}
public class UsuersViewModel : Consultation {
    public string Sex {get;set;}
}
public class DataBase {
    public IEnumerable<Consultation> CONSULT {
        get { 
            return new List<Consultation>{
                new Consultation { IdUser = 1, DateCreate=DateTime.Today, IdTypeConsult = 2},
                new Consultation { IdUser = 2, DateCreate=DateTime.Today.AddDays(1), IdTypeConsult = 4}
            };
        }}
    public IEnumerable<UsuersViewModel> USER {
        get {
            return new List<UsuersViewModel>{
                new UsuersViewModel { IdUser = 1, Sex="M"},
                new UsuersViewModel { IdUser = 1, Sex="M"},
                new UsuersViewModel { IdUser = 2, Sex="F"},
                new UsuersViewModel { IdUser = 2, Sex="F"}
            };
        }}
}

结果如下:

于 2014-03-14T13:06:10.153 回答