[英]Get distinct rows in Gridview based on One column
Hello I am trying to bind a gridview with distinct rows from the DB. 您好,我正在尝试将gridview与数据库中的不同行绑定在一起。 I want the distict to be implemented on JobID column. 我希望在JobID列上实现Distict。 Please help 请帮忙
using (ATPDataContext at = new ATPDataContext())
{
var qryPartnerJobs = (from pj in at.jobs
join jq in at.job_quotes on pj.JobID equals jq.JobID
join u in at.users on jq.TradeUserID equals u.UserID
where pj.IsApproved == true
select new
{
JobID = pj.JobID,
FirstName = u.FirstName,
ServiceName = pj.service.ServiceName,
ServiceTypeName = pj.service_type.ServiceTypeName,
IsApproved = pj.IsApproved,
IsActive = pj.IsActive,
IsQuoted = pj.IsQuoted,
IsAssigned = pj.IsAssigned,
ApprovalDate = pj.ApprovalDate,
Description = pj.Description
}).Distinct();
gvPartnerJob.DataSource = qryPartnerJobs;
gvPartnerJob.DataBind();
}
Distinct
accepts a EqualityComparer<T>
as it's parameter. Distinct
接受EqualityComparer<T>
作为其参数。 You can implement requested functionality using following comparer: 您可以使用以下比较器实现请求的功能:
public class JobComparer : EqualityComparer<Job>
{
public override bool Equals(Job x, Job y)
{
return x.JobID == y.JobID;
}
public override int GetHashCode(Job obj)
{
return obj.JobID.GetHashCode();
}
}
With this comparer, simply use Distinct(new JobComparer())
on your collection. 使用此比较器,只需在集合上使用Distinct(new JobComparer())
。
If you want the distinct to be performed in SQL, use GroupBy() 如果要在SQL中执行区分,请使用GroupBy()
var qryPartnerJobs = (from pj in at.jobs
join jq in at.job_quotes on pj.JobID equals jq.JobID
join u in at.users on jq.TradeUserID equals u.UserID
where pj.IsApproved == true
select new
{
JobID = pj.JobID,
FirstName = u.FirstName,
ServiceName = pj.service.ServiceName,
ServiceTypeName = pj.service_type.ServiceTypeName,
IsApproved = pj.IsApproved,
IsActive = pj.IsActive,
IsQuoted = pj.IsQuoted,
IsAssigned = pj.IsAssigned,
ApprovalDate = pj.ApprovalDate,
Description = pj.Description
})
// get a group for each distinct jobId
.GroupBy(t => t.JobID)
// select the first entry from each group
.SelectMany(g => g.Take(1));
If you'd like for the distinct operation to be performed in memory instead, you could use the Distinct() overload that allows you to pass an IEqualityComparer. 如果您希望在内存中执行不同的操作,则可以使用Distinct()重载,该重载允许您传递IEqualityComparer。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.