var result = db.SETUP_MASTER_LOGIN
.Where(x => x.DELETED == false)
.Select(s => new MasterLoginResp
{
resource_name = s.RESOURCE_NAME,
user_name = x.USER_NAME,
created_by = x.CREATED_BY
})
.ToList()
How to get distinct values from selected three columns? I have tried all the given solutions but did not get the required results.
2
Answers
I assume that MasterLoginResp is DTO object.
Now just use the way you do.
Note: This is client-side evaluation so it will bring all data from server.
This is an outstanding question. The Distinct method ought to optionally take a lambda function like
First()
,Count()
and others.The source code is public. Examining it and looking at how one would do this so as to remain IQueryable for delegation to the server where possible, we arrive at this code.
Alas,
First
returnsIEnumerable<T>
scuttling hopes of delegation.We can then apply this.
Given that you are already reducing the result to the columns on which you want to apply distinct, you might think you could just use
Distinct()
without parameters. However, this may directly compare the objects. You’ll have to be careful with that. My method will always work due to the implementation.Notice that I apply
Distinct
beforeSelect
reduces. This allows it to operate on the full gamut of properties, not just the ones you return fromSelect
.