使用C#将两个排序结果合并到linq中的一个结果中

时间:2015-02-02 10:36:41

标签: c# linq

我订购了两种类型的查询。我想把结果显示为一个。如果第一个查询和第二个查询都有计数,则这两个都是合并并显示一个结果。所以我创建了3个列表,如 jobs jobs1 jobs2 。我正在将值加入 jobs1 和jobs2。然后我将工会分配到 jobs3

代码

IQueryable<Job> jobs = _repository.GetJobs();
IQueryable<Job> jobs1 = _repository.GetJobs();
IQueryable<Job> jobs2 = _repository.GetJobs();

List<int> lstId = null;
List<int> lstUpdatedListId = null;
List<int> lstConId=null;
var order = _db.GetOrderDetails().Where(od => od.Masters.Id != null && od.OrderId == od.Master.OrderId && od.Master.Status == true && od.ValidityTill.Value >= currentdate).OrderByDescending(od => od.ValidityTill).Select(ord => ord.Master.Id.Value);
var order1 = _vasRepository.GetOrderDetails().Where(od => od.Masters.ConId != null && od.OrderId == od.Masters.OrderId && od.Masters.PaymentStatus == true && od.ValidityTill.Value >= currentdate).OrderByDescending(od => od.ValidityTill).Select(ord => ord.Masters.ConId.Value);
var updatedVacancyList = _repository.GetJobs().Where(c => c.UpdatedDate != null && updateFresh <= c.UpdatedDate).Select(c => c.Id);

 if (order1 .Count() > 0)
        {
            lstConId = order1.ToList();

            Func<IQueryable<Job>, IOrderedQueryable<Job>> orderingFunc = query =>
            {
                if (order1.Count() > 0)
                    return query.OrderByDescending(rslt => lstConId.Contains(rslt.Con.Id)).ThenByDescending(rslt=>rslt.CreatedDate);
                else
                    return query.OrderByDescending(rslt => rslt.CreatedDate);
            };

            jobs1 = orderingFunc(jobs);

        }

if (order.Count() > 0)
        {
            lstId = order.ToList();
            lstUpdatedJobsListId = updatedVacancyList.ToList();

            Func<IQueryable<Job>, IOrderedQueryable<Job>> orderingFunc = query =>
            {
                if (order.Count() > 0)
                    return query.OrderByDescending(rslt => lstId.Contains(rslt.Id)).ThenByDescending(rslt => lstUpdatedJobsListId.Contains(rslt.Id)).ThenByDescending(rslt=>rslt.CreatedDate);
                if (updatedVacancyList.Count() > 0)
                    return query.OrderByDescending(rslt => lstUpdatedJobsListId.Contains(rslt.Id)).ThenByDescending(rslt => rslt.UpdatedDate);
                else
                    return query.OrderByDescending(rslt => rslt.CreatedDate);
            };

        jobs2 = orderingFunc(jobs);
      }

        jobs = jobs1.Union(jobs2);

我在运行应用程序时遇到错误,如下所示,

无法将文本数据类型选为DISTINCT,因为它无法比较。

我需要帮助来纠正这个问题。我也想下降。

1 个答案:

答案 0 :(得分:0)

数据库中的一个列是“文本”类型。将其转换为varchar(MAX)