【问题标题】:Linq query rewritingLinq 查询重写
【发布时间】:2016-02-08 12:26:58
【问题描述】:

这真的把我难住了......

我有两个问题..

这个不工作,有错误, “LINQ to Entities 无法识别方法 GetAllCustomers()' 方法,并且该方法无法转换为商店表达式。”

results = theDocuments.Select(x => new DocumentModel()
                {
                    document_id = x.document_id,
                    document_type = x.document_type,
                    franchisee_id = x.franchisee_id,
                    customer_id = x.customer_id,
                    address = x.address,
                    area_id = x.area_id,
                    ***HERE***customer_name = outletService.GetAllCustomers().Where(xx => xx.id == x.customer_id).First().name
                }).OrderBy(x => x.document_id);

现在像这样重写它:

results = from p in theDocuments
                          join cust in outletService.GetAllCustomers() on p.customer_id equals cust.id
                          select new DocumentModel()
                          {
                              customer_name = cust.name,
                              document_id = p.document_id,
                              document_type = p.document_type,
                              franchisee_id = p.franchisee_id,
                              customer_id = p.customer_id,
                              address = p.address
                          };

我知道这不能转换为它的相关 SQL,但是,我不确定第二个查询在执行时有什么不同以使其工作,它是否在执行任何其他操作之前执行 GetAllCustomers()?它在内部的工作方式有何不同?

如何重写第一个查询以使其正确执行?

谢谢, 詹姆斯。

**这就是我最终得到的结果**

 results = theDocuments.Join(
                    outletService.GetAllCustomers(), docs => docs.customer_id, customers => customers.id, (doc, cust) => new DocumentModel()
                    {
                        document_id = doc.document_id,
                        document_type = doc.document_type,
                        franchisee_id = doc.franchisee_id,
                        customer_id = doc.customer_id,
                        address = doc.address,
                        area_id = doc.area_id,
                        customer_name = cust.name
                    });

【问题讨论】:

  • join 使用Join 方法,这就是区别
  • 如果你声明了一个像 var Name = outletService.GetAllCustomers().Where(xx => xx.id == x.customer_id).First().name 这样的变量然后在你的第一个查询中使用它,它会起作用。显然,您需要先获得 Name,然后再将其与 lambda 表达式 一起使用

标签: c# linq linq-to-entities iqueryable


【解决方案1】:

第一个尝试将您的方法转换为 SQL 方法,第二个尝试在查询中创建连接。在我看来,正确的方法是使用 join :)

【讨论】:

  • 非常感谢!我想我希望它知道我的加入是我要求它做的太多了。
【解决方案2】:

我是个问题。我找到了解决方案。这可能对你有帮助。

            dynamic user = new
            {
                Name = "",
                Surname = ""
            };

            dynamic userModel = new
            {
                Name = "",
                Surname = "",
                FullName = ""
            };

            var query = db.User.AsNoTracking();
            query = query.Select(x => new userModel
            {
                Name = x.Name,
                Surname = x.SurName,
                FullName = $"{x.Name}{x.SurName}",
            }).ToList();

如果您尝试执行此查询。你会崩溃的。 Linq 不支持

FullName = $"{x.Name}{x.SurName}"

您必须将此行更改为

全名 = x.Name + " " + x.SurName

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    相关资源
    最近更新 更多