Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i want select distinct firstName's column from Database with linq
my class is:

public partial class user
{

    public string firstName{ get; set; }
    public string lastName{ get; set; }
    public int    dgree{ get; set; }
    public string class{ get; set; }
}

i using below code but return full list.

var query = (from user _db.Users
                     select user).Distinct().OrderBy(u=>u.firstName).ToList();
share|improve this question
 
Distinct on a specific column? –  Magnus Jul 13 at 8:00
 
firstName column –  Ramin Asadi Jul 13 at 9:04

2 Answers

up vote 1 down vote accepted

To return distinct values from a list of custom objects you need to impelement the IEquatable<T> interface in the user class.

 public partial class user : IEquatable<user>
 {

public string firstName{ get; set; }
public string lastName{ get; set; }
public int    dgree{ get; set; }
public string class{ get; set; }

 public bool Equals(Product other)
{

   //check if the objects are the same based on your properties:
   //example 
    if (firstName == other.firstName) 
            return true; //then i assume they're equal and return true. You can check here all your properites that make an object unique. 

   return false; 
}

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

  }`

More examples at : http://msdn.microsoft.com/en-us/library/bb348436.aspx

share|improve this answer

The way you wrote it you get only exact duplicate lines cut. do you get any of them in your result ?

if you need to distinct on specific column check out this answer: Distinct By specific property

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.