0

您好,我正在尝试将 gridview 与数据库中的不同行绑定。我希望在 JobID 列上实现 distict。请帮忙

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();

        }
4

2 回答 2

0

Distinct接受 aEqualityComparer<T>作为它的参数。您可以使用以下比较器实现请求的功能:

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();
    }
}

使用此比较器,只需Distinct(new JobComparer())在您的收藏中使用。

于 2013-06-22T14:33:45.137 回答
0

如果要在 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));

如果您希望在内存中执行不同的操作,则可以使用允许您传递 IEqualityComparer 的 Distinct() 重载。

于 2013-06-22T18:53:25.860 回答