21

Can someone help me translate the following SQL query into a LINQ format.

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'

Many Thanks

stats101
  • 1,837
  • 9
  • 32
  • 50
  • "The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'Join'." :S – stats101 Feb 14 '12 at 13:07
  • Well, what are the types of `DGApprovedLinks.ResponseCode` and `DGConfigs.SubType`? – Blorgbeard Feb 14 '12 at 13:10
  • ResponseCode is int and subtype in string. – stats101 Feb 14 '12 at 13:12
  • Just did a ToString, and the error is no longer showing. Thanks! – stats101 Feb 14 '12 at 13:13
  • Actually. It's not working. It's not correctly comparing the int value with the string value. Using following approach: SqlFunctions.StringConvert((double)a.ResponseCode) equals c.SubType – stats101 Feb 14 '12 at 14:11
  • Why are you casting to double? Doesn't `a.ResponseCode.ToString() equals c.SubType` work? – Blorgbeard Feb 14 '12 at 14:22
  • Nope. It complains that ToString isn't supported by LINQ. – stats101 Feb 14 '12 at 14:32
  • OK. All I can suggest is to use your `StringConvert` code, and run SQL Profiler on it to see what SQL it generates. – Blorgbeard Feb 14 '12 at 14:35
  • the following is run on the server: – stats101 Feb 14 '12 at 15:38
  • SELECT [Extent1].[ID] AS [ID], [Extent1].[HostID] AS [HostID], [Extent2].[URL] AS [URL], [Extent1].[SourceURL] AS [SourceURL], [Extent1].[TargetURL] AS [TargetURL], [Extent3].[Value] AS [Value], [Extent1].[ExtFlag] AS [ExtFlag] FROM [dbo].[DGApprovedLink] AS [Extent1] INNER JOIN [dbo].[DGHost] AS [Extent2] ON [Extent1].[HostID] = [Extent2].[ID] INNER JOIN [dbo].[DGConfig] AS [Extent3] ON ((STR([Extent1].[ResponseCode])) = [Extent3].[SubType]) OR ((STR( CAST( [Extent1].[ResponseCode] AS float)) IS NULL) AND ([Extent3].[SubType] IS NULL)) WHERE N'HTTP Status' = [Extent3].[Type] – stats101 Feb 14 '12 at 15:38

2 Answers2

45

I think it would be something like:

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 };
Alexey Raga
  • 7,457
  • 1
  • 31
  • 40
  • 1
    Many thanks. I tried it, and I get an error on the 3rd join saying "The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'Join'." – stats101 Feb 14 '12 at 13:06
  • 2
    Working now. Had to use a ToString function for one of the comparisons. Thanks. – stats101 Feb 14 '12 at 13:13
  • Actually. It's not working. It's not correctly comparing the int value with the string value. Using following approach: SqlFunctions.StringConvert((double)a.ResponseCode) equals c.SubType – stats101 Feb 14 '12 at 14:10
  • 7
    Really?! How you expect it to compare "correctly" values with different types?! You never specified types, so how can we know them? The query should work perfectly in terms of LINQ, juts deal with your types appropriately. – Alexey Raga Feb 16 '12 at 06:03
  • 1
    I am getting an error on the 3rd join, a red squiggle saying the type of one of the expression..." very much like the same on first comment – Alexander Jan 30 '20 at 04:25
  • @AlexeyRaga nice, exactly what I needed thank you from the future. – nulltron Feb 23 '21 at 19:29
-1
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);

        }

    }
}
  • 9
    Without an explanation as to what all of this does and how it relates to what the person asking wants to know, this is a very poor answer. – MattD Nov 04 '15 at 20:05