为什么不能使用Lambda表达式从集合中选择特定的列?

时间:2019-05-14 05:54:15

标签: c# entity-framework lambda ef-code-first

使用lambda表达式,我只想选择2列,但会引发错误。

代码:

 public List<Certificates> GetClientsList(string certificationNo = "")
        {
            List<Certificates> certificatesList = new List<Certificates>();
            var query = uow.CertificatesRepository.GetQueryable().AsQueryable();
            if (!string.IsNullOrEmpty(certificationNo))
            {
                query = query.Where(x => x.CertificationNo.Contains(certificationNo)).Select(n => new { ClientName= n.Client, ID= n.CertificatesID});
            }
            certificatesList = query.ToList();

            return certificatesList;
        }

证书类别:

public class Certificates
    {
        public int CertificatesID { get; set; }
        public string FileName { get; set; }

        [Required]
        [Display(Name = "Certification No")]
        public string CertificationNo { get; set; }

        [Required]
        [Display(Name = "Issue Date")]
        public string IssueDate { get; set; }

        [Required]
        public string Details { get; set; }

        [Required]
        public string Client { get; set; }
    }

错误:

无法将匿名可查询类型转换为列表

3 个答案:

答案 0 :(得分:0)

您可能正在寻找类似的东西(取决于CertificatesRepository的返回类型)

public List<Certificates> GetClientsList(string certificationNo = "")
{
   var query = uow.CertificatesRepository;

   if (string.IsNullOrEmpty(certificationNo))
      return query.ToList();

   return query.Where(x => x.CertificationNo.Contains(certificationNo))
               .ToList();
}

更新

  

只需要选择2列

public List<Certificates> GetClientsList(string certificationNo = "")
{
   var query = uow.CertificatesRepository;

   if (string.IsNullOrEmpty(certificationNo))
      return query.Select(n => new Certificates { ClientName = n.Client, ID = n.CertificatesID})
                  .ToList();

   return query.Where(x => x.CertificationNo.Contains(certificationNo))
               .Select(n => new Certificates { ClientName = n.Client, ID = n.CertificatesID})
               .ToList();
}

public List<(int ID , string ClientName)> GetClientsList(string certificationNo = "")
{
   var query = uow.CertificatesRepository;

   if (string.IsNullOrEmpty(certificationNo))
      return query.Select(n => (ID = n.CertificatesID, ClientName = n.Client))
                  .ToList();

   return query.Where(x => x.CertificationNo.Contains(certificationNo))
               .Select(n => (ID = n.CertificatesID, ClientName = n.Client))
               .ToList();
}

答案 1 :(得分:0)

为什么需要先将其转换为Queryable? uow.CertificatesRepository.GetQueryable()返回什么类型?

public List<Certificates> GetClientsList(string certificationNo = "")
        {
            var query = uow.CertificatesRepository.GetQueryable(); // do ToList here if it is IQuerable, but as it seems it was not.

            return query.Where(x=>x.CertificationNo.Contains(certificationNo)).Select(x=> new Certificates(){ClientName= n.Client, ID= n.CertificatesID}).ToList();
        }

答案 2 :(得分:0)

您的Select返回匿名对象。这些匿名对象当然不能转换为证书。

如果您真的想使用自己的Certificates类,则可以在选择的内容中创建新对象:

public List<Certificates> GetClientsList(string certificationNo = "")
{
   List<Certificates> certificatesList = new List<Certificates>();
   var query = uow.CertificatesRepository.GetQueryable().AsQueryable();
   if (!string.IsNullOrEmpty(certificationNo))
   {
       query = query.Where(x => x.CertificationNo.Contains(certificationNo)).Select(n => new Certificates{ Client = n.Client, CertificatesID = n.CertificatesID});
   }
   certificatesList = query.ToList();
   return certificatesList;
}

更好的解决方案可能是创建一个仅包含两个必需属性的新类。

第三种可能性是返回List<dynamic>。但是然后您就不再需要强类型了。