【问题标题】:Doing multiple joins within a LINQ statement在 LINQ 语句中执行多个连接
【发布时间】:2012-02-14 11:58:15
【问题描述】:

谁能帮我把下面的 SQL 查询翻译成 LINQ 格式。

SELECT a.ID,
       a.HostID,
       h.URL,
       a.SourceURL,
       a.TargetURL,
       c.Value,
       a.ExtFlag
FROM Link a
INNER JOIN Host h
ON h.ID = a.HostID
INNER JOIN Ref c
ON a.ResponseCode = c.SubType
AND c.Type = 'HTTP Status'

非常感谢

【问题讨论】:

  • "join 子句中的表达式类型不正确。调用 'Join' 时类型推断失败。" :S
  • 那么DGApprovedLinks.ResponseCodeDGConfigs.SubType的类型是什么?
  • ResponseCode 是 int 和 string 子类型。
  • 刚做了一个ToString,错误不再显示。谢谢!
  • 其实。它不工作。它没有正确地将 int 值与字符串值进行比较。使用以下方法: SqlFunctions.StringConvert((double)a.ResponseCode) 等于 c.SubType

标签: c# .net linq asp.net-mvc-3 entity-framework


【解决方案1】:

我想应该是这样的:

var result = from a in Context.DGApprovedLink 
             join h in Context.DGHost on a.HostID equals h.ID
             join c in Context.DGConfig on a.ResponseCode equals c.SubType
             where c.Type == "HTTP Status"
             select new {
                 a.ID,
                 a.HostID,
                 h.URL,
                 a.SourceURL,
                 a.TargetURL,
                 c.Value,
                 a.ExtFlag };

【讨论】:

  • 非常感谢。我试过了,在第三次加入时出现错误,提示“加入子句中的一个表达式的类型不正确。在调用 'Join' 时类型推断失败。”
  • 现在工作。必须使用 ToString 函数进行比较之一。谢谢。
  • 其实。它不工作。它没有正确地将 int 值与字符串值进行比较。使用以下方法: SqlFunctions.StringConvert((double)a.ResponseCode) 等于 c.SubType
  • 真的吗?!您如何期望它将“正确”的值与不同类型的值进行比较?!你从来没有指定类型,那么我们怎么知道它们呢?查询应该在 LINQ 方面完美运行,只是适当地处理您的类型。
  • 我在第 3 次加入时遇到错误,红色曲线表示其中一个表达式的类型..." 与第一条评论非常相似
【解决方案2】:
Create Unit test class using MStest and copy the code 

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace LinqTest.Test
{

    public class Employee
    {
        public int EmpId { get; set; }
        public string Name { get; set; }
        public DateTime? DOB { get; set; }
        public decimal Salary { get; set; }
        public DateTime DOJ { get; set; }
        public bool IsActive { get; set; }
    }


    public class Book
    {
        public int BookId { get; set; }
        public string Title { get; set; }
        public double Price { get; set; }
    }

    public class BookOrder
    {
        public int OrderId { get; set; }
        public int EmpId { get; set; }
        public int BookId { get; set; }
        public int Quantity { get; set; }
    }



    [TestClass]
    public class Linqtest
    {
        List<Employee> Employees;
        List<Book> Books;
        List<BookOrder> Orders;

        [TestInitialize]
        public void InitializeData()
        {
            Employees = new List<Employee>();
            Books = new List<Book>();
            Orders = new List<BookOrder>();

            Employees.Add(new Employee(){EmpId = 1, Name ="Test1" ,  DOB = new DateTime(1980,12,15),IsActive = true,Salary = 4500});
            Employees.Add(new Employee() { EmpId = 11, Name = "Test2", DOB = new DateTime(1981, 12, 15), IsActive = true, Salary = 3500 });
            Employees.Add(new Employee() { EmpId = 5, Name = "Test3", DOB = new DateTime(1970, 2, 15), IsActive = true, Salary = 5500 });
            Employees.Add(new Employee() { EmpId = 8, Name = "Test4", DOB = new DateTime(1978, 1, 15), IsActive = true, Salary = 7500 });
            Employees.Add(new Employee() { EmpId = 9, Name = "Test5", DOB = new DateTime(1972, 2, 5), IsActive = true, Salary = 2500 });
            Employees.Add(new Employee() { EmpId = 10, Name = "Test6", DOB = new DateTime(1980, 10, 8), IsActive = false, Salary = 5500 });
            Employees.Add(new Employee() { EmpId = 15, Name = "Test7", DOB = new DateTime(1983, 11, 25), IsActive = true, Salary = 3500 });

            Books.Add(new Book(){BookId = 2, Price = 24.99,Title = "British Food"});
            Books.Add(new Book() { BookId = 5, Price = 4.99, Title = "Holidays in UK" });
            Books.Add(new Book() { BookId = 7, Price = 7.99, Title = "UK Laws" });


            Orders.Add(new BookOrder(){EmpId = 1,OrderId = 1,BookId = 2,Quantity = 3});
            Orders.Add(new BookOrder() { EmpId = 1, OrderId = 1, BookId = 5, Quantity = 1 });
            Orders.Add(new BookOrder() { EmpId = 1, OrderId = 2, BookId = 7, Quantity = 5 });
            Orders.Add(new BookOrder() { EmpId = 11, OrderId = 3, BookId = 2, Quantity = 3 });
            Orders.Add(new BookOrder() { EmpId = 11, OrderId = 4, BookId = 7, Quantity = 3 });
        }


        [TestMethod]
        public void CheckEmpCount()
        {

            var res = Employees
                .Where(e => e.EmpId > 5)
                .Where(t =>t.Salary>=5000);

            Assert.AreEqual(2,res.Count());

            res = Employees
                .Where(e => e.EmpId > 5);

            Assert.AreEqual(5,res.Count());

        }

        [TestMethod]
        public void TestGroupBy()
        {
            var res = from e in Employees
                group e by e.Salary;

            Assert.AreEqual(5,res.Count());

            var res1 = Employees.GroupBy(e => e.Salary);
            Assert.AreEqual(5, res1.Count());
        }

        [TestMethod]
        public void TestJoin()
        {
            var res = from o in Orders
                join Employee e in Employees
                    on o.EmpId equals e.EmpId
                where o.EmpId == 11
                select o;

            Assert.AreEqual(2,res.Count());
        }

        [TestMethod]
        public void TestJoinData()
        {
            var res = from o in Orders
                join Employee e in Employees
                    on o.EmpId equals e.EmpId 
                join Book b in Books
                    on o.BookId equals b.BookId 
                orderby e.EmpId
                select new {o.OrderId, e.Name, b.Title, b.Price};

            Assert.AreEqual("Test1", res.First().Name);

        }

    }
}

【讨论】:

  • 如果没有解释所有这一切的作用以及它与提问者想知道的内容有何关系,这是一个非常糟糕的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-14
  • 1970-01-01
  • 1970-01-01
  • 2016-06-21
  • 2014-06-23
相关资源
最近更新 更多