简体   繁体   中英

Convert this SQL query into Linq

I have ac# webforms application that uses Linq2SQL, SQL Server, .net 4.5

I know SQL reasonably well, but am finding Linq very hard work.

SQL although tricky usually makes sense, but I find linq almost impossible to write unless I can find a identical example online. This time I cannot find one.

I am using a view within the SQL to simplify the process and cut out the need for insane linq syntax joining tables together, however as views won't allow ORDER BY statements I am a bit stuck. I can get the query to work perfectly in a stored procedure, but I gave up trying to get the data back through linq after 5-hours scouring of the 'Net to find out how.

The original ORDER BY sorts the Name column but ignores a "The " at the start of the field giving:

Apple
The Banana
Orange
Pear

Here is the SQL query:

SELECT TOP 100 Name, Year, Data1, Data2
FROM v_List_Items
ORDER BY 
    CASE WHEN LOWER(SUBSTRING(Name, 1, 4)) = 'the ' 
           THEN SUBSTRING(Name, 5, len(Name)) 
           ELSE NAME 
    END ASC

Any ideas ?

Also being able to change the 100 results filter dynamically would be useful.

Thanks

I wrote the following sql insert code into a table

INSERT INTO NamedFruit(Name) Values('Apple')
GO
INSERT INTO NamedFruit(Name) Values('The Banana')
GO
INSERT INTO NamedFruit(Name) Values('Orange')
GO
INSERT INTO NamedFruit(Name) Values('Pear')

used the following query

NamedFruits.OrderBy(s=>s.Name.StartsWith("The ") ? s.Name.Substring(4) : s.Name);

results:

Apple 
The Banana 
Orange 
Pear 

Should be no issue.

Generated sql was:

-- Region Parameters
DECLARE @p0 VarChar(1000) = 'The %'
DECLARE @p1 Int = 4
-- EndRegion
SELECT [t0].[Name]
FROM [NamedFruit] AS [t0]
ORDER BY 
    (CASE 
        WHEN [t0].[Name] LIKE @p0 THEN CONVERT(NVarChar,SUBSTRING([t0].[Name], @p1 + 1, CONVERT(Int,DATALENGTH([t0].[Name]))))
        ELSE CONVERT(NVarChar,CONVERT(NVarChar(20),[t0].[Name]))
     END)

Not sure if this will work with Ling to SQL, but it should give you a start. I'm using a ternary operator to determine the string to use for the sort.

var fruits = new List<string> 
{
    "Apple",
    "The Banana",
    "Orange",
    "Pear"
};

var orderedFruits = fruits
    .OrderBy(s => 
        s.StartsWith("The ") 
            ? s.Substring(4) 
            : s);

You can simply execute the SQL within LINQ, there's no need to translate. For example:

Northwnd db = new Northwnd(@"c:\northwnd.mdf");
IEnumerable<Customer> results = db.ExecuteQuery<Customer>
(@"SELECT c1.custid as CustomerID, c2.custName as ContactName
    FROM customer1 as c1, customer2 as c2
    WHERE c1.custid = c2.custid"
);

Source: How to: Directly Execute SQL Queries

This is better because you have no guarantees that a particular piece of LINQ generates a particular SQL query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM