【问题标题】:Converting Linq query to Lambda将 Linq 查询转换为 Lambda
【发布时间】:2016-09-06 08:45:54
【问题描述】:

如何将以下 linq to sql 转换为 lambda 表达式。谢谢。

 var existing = (from a in db.sms_imported_trn_code
                 where !db.sms_description.Any(m => m.trn_code == a.trn_code)
                 select new  { trn_code = a.trn_code }).ToList();

编辑:

我尝试了以下查询,但它给了我一个错误。请查看下面的图片。

我的模型类如下

    public class sms_imported_trn_code
    {
        public int id { get; set; }
        public string trn_code { get; set; }
        List<sms_imported_trn_code> sms_import_list { get; set; }
    }
}


 public class sms_description
    {

        public int id { get; set; }
        public string trn_code { get; set; }
        public string Description { get; set; }

        [DisplayFormat(DataFormatString = "{0:MMM-dd-yyyy}", ApplyFormatInEditMode = true)]
        public DateTime create_date { get; set; }
        public string created_by { get; set; }

    }

【问题讨论】:

  • 为什么需要这样做?
  • 在询问之前您尝试/研究了什么?这可能会对您有所帮助:codeblog.jonskeet.uk/2011/01/28/…
  • 阅读How to Ask 并分享您对实际错误的研究。如果没有错误,我们将无法做很多事情。
  • 现在他已经为我的解决方案添加了error。你现在可以帮我吗? :D @CodeCaster
  • 顺便说一句,你为什么使用 Any (!db.sms_description.Any) 的反面,而不是 All

标签: c# entity-framework lambda linq-to-sql


【解决方案1】:

您收到错误是因为您正在编写“s.sms_description”,sms_description 不是类 sms_imported_trn_code 的一部分,它是一个单独的类,因此您需要编写 db.sms_description

db.sms_imported_trn_code
  .Where(s => !db.sms_description.Any(m => m.trn_code == s.trn_code))
  .Select(t => new {trn_code = t.trn_code }).ToList();

【讨论】:

  • 谢谢它的工作。除了实体框架,您还推荐哪些其他框架 ORM?或者还是微软推荐的。
  • 我没有太多其他框架的经验,我更喜欢使用实体框架。
猜你喜欢
  • 2018-09-23
  • 2021-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多