简体   繁体   中英

How to get distinct values using LINQ?

Hello i got a table like this=>

    stateID    requestNo    reqStateID       reqStateDate
       1          1             13        03.01.2012 10:57
       2          1              3        03.01.2012 10:58
       3          2              3        03.01.2012 11:14
       4          2              3        03.01.2012 11:15
       5          1              5        03.01.2012 22:28
       6          1              7        05.01.2012 14:54
       7          3              3        05.01.2012 14:55

I need to get, last added(reqStateDate) unique Requests No (requestNo). I tried something like this but it's not work..

    public List<ReqStates> GetRequests(int reqStateID)
    {
        return  (from rs in db.ReqStates
                          where rs.reqStateID== reqStateID
                          orderby rs.reqStateDate descending
                          select rs).Distinct().ToList();            
    }

if parameter (reqStateID) takes 3, I have to get 2 requests. requestNo = 2 and requestNo = 3. Because, both of request's reqstateID is 3 and their added dates the latest. RequestNo=1=> the last added status is 13. This is why it(1) should not come.

I hope someone can help me, and shows an easy way.

You have to tell Distinct by what property it should distinct the values. The default equality comparer will be used to distinct the values if nothing is specified.

You have two options here:

  1. Pass an IEqualityComparer to distinct.
  2. Implement IEquatable on the ReqStates .

Example:

class ReqStatesComparer: IEqualityComparer<ReqStates>
{
  public bool Equals(ReqStates a, ReqStates b)
  {
    return a.requestNo == b.requestNo;
  }

  public int GetHashCode(ReqStates rs)
  {
    return rs.GetHashCode();
  }
}


    public List<ReqStates> GetRequests(int reqStateID)
    {
        return  (from rs in db.ReqStates
                          where rs.reqStateID== reqStateID
                          orderby rs.reqStateDate descending
                          select rs).Distinct(new ReqStatesComparer()).ToList();            
    }

I'm not sure exactly what result your looking for but your doing a distinct on the whole object. So the object must be distinct across all fields.

Try below where you're just selecting a field and not the whole object.

Then modify to only use the fields you need.

Try this

return  (from rs in db.ReqStates
                      where rs.reqStateID== reqStateID
                      orderby rs.reqStateDate descending
                      select rs.requestNo).Distinct().ToList();   

It sounds like you want to retrieve the latest requests by requestNo with the latest reqStateDate given an ID. If this is the case, you can do:

public List<ReqStates> GetRequests(int reqStateID)
{
    return db.ReqStates.Where(rs => rs.reqStateID == reqStateID)
                       .GroupBy(rs => rs.requestNo)
                       .Select(g => g.OrderByDescending(rs => rs.reqStateDate).First()) 
                       .ToList();            
}

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