SQL to LINQ Tool
Edit 7/17/2020: I cannot delete this accepted answer. It used to be good, but now it isn't. Beware really old posts, guys. I'm removing the link.
[Linqer] is a SQL to LINQ converter tool. It helps you to learn LINQ and convert your existing SQL statements.
Not every SQL statement can be converted to LINQ, but Linqer covers many different types of SQL expressions. Linqer supports both .NET languages - C# and Visual Basic.
LinqPad - Convert SQL to Linq command
In general there are no tools to covert SQL to Linq as @andres-abel mention before, but sometimes you have to write Linq that will execute exactly as specified SQL (for example because of performance issues, backward compatability or some other reasons).
In this case I'll advice you to do reverse engineering by yourself:
- configure logging of dump SQL statements generated by Linq to
stdout
usingObjectQuery.ToTraceString
,DbCommand.CommandText
,- logger availabe to your data source
- manually rewrite Linq statement until you'll get what you need
Converting a SQL to Linq Query
Bit complex query, giving my best to make it work.
var result = table.AsEnumerable().Where(u=> u.Field<DateTime>("createdTime") > DateTime.Now.AddDays(-7)) //subtract a week
.GroupBy(g=> new { userid = g.Field<string>("userId") , span = g.Field<DateTime>("createdTime").Minute })
.Select(g=> new { userid = g.Key.userid, count = g.Count()})
.GroupBy(g=> g.userid ).Select(s=> new {userid = s.Key, count = s.Count()});
Working Demo
Convert SQL to LINQ to SQL
See this existing thread.
If you decide to do it by hand, Linqpad should be useful.
Assistance to convert my SQL query to LINQ?
I believe this should be equivalent:
var productType = "Yarn";
var productId = 2835;
var query =
from st in ctx.StockDetails
where st.ProductType == productType
where st.ProductId == productId
orderby st.CreationDate descending
let suppliers =
from suo in ctx.SupplierOrders
join su in ctx.Suppliers on suo.SupplierId equals su.Id
where suo.Id == st.SupplierCmdId
select su
from su in suppliers.DefaultIfEmpty()
select new
{
st.ProductId,
st.ProductType,
st.StockValue,
st.InOut,
st.SupplierCmdId,
st.CreationDate,
SupplierId = su.Id,
};
Convert SQL to LINQ Query
Have you tried Linqer http://www.sqltolinq.com
An SQL-> LINQ converter..
Or LINQPad
http://www.linqpad.net/
convert sql to linq
One of good tool to convert SQL to Linq : Linqer
Try out this query
var q = (from r in Room
join v in Reservation on r.RoomID equals v.RoomID into outer
from o in outer.DefaultIfEmpty()
where !(o.Date_Check_Out<= startdate || o.Date_Check_In>=endDate)
&& v.cancel == 0 && v.ReservationID == null
select r);
Also check this :
See SQL to LINQ Tool
existing thread.
If you decide to do it by hand, Linqpad should be useful.
You also like to see : SQL to LINQ ( Visual Representation ) some good exaple by graphical representation...
Related Topics
How to Populate a Table With a Range of Dates
MySQL Query Group by Day/Month/Year
MySQL - How to Unpivot Columns to Rows
Difference Between Exists and in in Sql
How to Randomly Select Rows in Sql
How to Create a Table from Select Query Result in SQL Server 2008
MySQL Select Only Not Null Values
MySQL Trigger After Update Only If Row Has Changed
How to Create a Calendar Table For 100 Years in Sql
How to Load a Large Number of Strings to Match With Oracle Database
Null in MySQL (Performance & Storage)
How to Avoid the "Divide by Zero" Error in Sql