Convert SQL to LINQ to SQL

匿名 (未验证) 提交于 2019-12-03 02:24:01

问题:

I'm having great trouble in converting the following SQL to LINQ to SQL, any one able to help out?

SELECT     dbo.ExpensesGroup.ExpenseGroupId, dbo.ExpensesGroup.Title, SUM(dbo.Expenses.Amount) AS TotalAmount, MAX(dbo.Expenses.DateLastTickled)                        AS LastTickledDate, MAX(dbo.ExpensesGroup.DateTime) AS Date, Username FROM         dbo.Expenses INNER JOIN                       dbo.ExpensesGroup ON dbo.Expenses.ExpenseId = dbo.ExpensesGroup.ExpensesId WHERE     dbo.Expenses.Status = 'AwaitingApproval' or dbo.Expenses.Status = 'AwaitingApprovelGrouping' GROUP BY dbo.ExpensesGroup.ExpenseGroupId, dbo.ExpensesGroup.Title, dbo.Expenses.Username ORDER BY MAX(dbo.ExpensesGroup.DateTime) DESC, dbo.ExpensesGroup.Title 

Or even better anyone know of an automatic SQl to LINQ converter?

回答1:

See this existing thread.

If you decide to do it by hand, Linqpad should be useful.



回答2:

I tried linqpad but its not for converting SQL to linq but its actual use is to replace sql with linq to query your database

however I think linqer is the right choice if you like to convert SQL query to LINQ. you can download it from their official site.

http://www.sqltolinq.com/



回答3:

linqpad don't convert.

free converter

http://windowsphone.interoperabilitybridges.com/articles/sql-to-linq-converter

Note: Run with SQLite in Windows, not WindowsPhone necesary



回答4:

To go from LINQ -> SQL try this:

        var db = new DBModelDataContext();         StringBuilder sb = new StringBuilder();         StringWriter sw = new StringWriter(sb);         db.Log = sw; //set the writer          var items = (from rec in db.Table1                      select rec).ToList();          var sql = sb.ToString(); //here is the SQL from LINQ. 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!