Sql To Csharp Linq Converter Online

Find the data you need here

We provide programming data of 20 most popular languages, hope to help you!

Previous PostNext Post

Convert SQL query into linq C# [closed]

SELECT   ts.TeacherId, count(Distinct ts.SubjectId) as Subjects
from dbo.TeacherSubjects ts
GROUP BY ts.TeacherId
HAVING ts.TeacherId  = 2352
var SubjectsGroup = db.TeacherSubjects.Where(p => p.TeacherId == 2352).Distinct().GroupBy(x => x.TeacherId);
var teacherId = ;  // your teacherId here

var result = new {
    TeacherId = teacherId,
    Subjects = db.TeacherSubjects.Where(ts => ts.TeacherId == teacherId).Select(x => x.SubjectId).Distinct().Count()
};
var teacherId = ;  // put your teacherId here
var result = 
    (from ts in TeacherSubjects.Where(x => x.TeacherId == teacherId)
    group ts by ts.SubjectId into gr
    select new 
    {       
        TeacherId = teacherId,
        Subjects = gr.Count()
    }).ToList();

Online sql to linq generator

Have you tried Linqer http://www.sqltolinq.com

An SQL-> LINQ converter..

Or LINQPad http://www.linqpad.net/
                                    CREATE PROCEDURE [dbo].[GetContactType]  
(  
     @ContactTypeId UNIQUEIDENTIFIER = NULL  
    ,@RowStatusId UNIQUEIDENTIFIER = '10000000-0000-0000-0000-000000000000'  
    ,@IncludeAuditFields BIT = 0  
    ,@ReturnCode INT = 0 OUTPUT  
)  
AS  
BEGIN;  
        SET NOCOUNT ON;  
        SET @ReturnCode = 0;  
        SELECT [dbo].[GetContactTypeXml]  
               (@ContactTypeId  
               ,@RowStatusId  
               ,@IncludeAuditFields) AS [Result];  
    RETURN @ReturnCode;  
END;  
  

Convert SQL to LINQ in c#

SELECT  
  dateentered,  
  sum(Advance) AS amount,  
  CAST(100 * sum(sum(Advance)) OVER (ORDER BY dateentered) 
     / sum(sum(Advance)) OVER () AS numeric(10, 2)) AS percentage 
FROM Portfolio WHERE [Date Funded]>='12/1/2015' and [Date Funded]<='11/30/2020' 
GROUP BY dateentered
    class Program
    {
        static void Main(string[] args)
        {
            Context db = new Context();

            var results = db.Portfolio.Where(x => (x.Date_Funded.Date >= DateTime.Parse("12/1/2015")) && (x.Date_Funded.Date <= DateTime.Parse("11/39/2020")))
                .OrderBy(x => x.dateentered)
                .GroupBy(x => x.dateentered.Date)
                .Select(x => new { dateentered = x.Key, sum = x.Sum(y => y.Advance) })
                .Select(x => new
                {
                    dateentered = x.dateentered,
                    amount = x.sum,
                    percentage = Math.Round((100 * x.sum), 2)
                }).ToList();

            
        }
    }
    public class Context
    {
        public List<Portfolio> Portfolio { get; set; }
    }
    public class Portfolio
    {
        public DateTime dateentered { get; set; }
        public decimal Advance { get; set; }
        public DateTime Date_Funded { get; set; }

    }

Convert SQL query to C# LINQ qu


select * from PatVisit p
inner join Device d on d.PattId = p.PatId and d.SyncDate is not null
where p.VisitDate in
( select max(VisitDate) from PatVisit pv where VisitId in (4,7,2,8,9) group by PatId )
and DATEDIFF(d,p.visitDate,GETDATE()) <10

select * from PatVisit p
inner join Device d on d.PattId = p.PatId and d.SyncDate is not null
where p.VisitDate in
( select max(VisitDate) from PatVisit pv where VisitId in (4,7,2,8,9) group by PatId )
and DATEDIFF(d,p.visitDate,GETDATE()) <10

DECLARE @finaldate DATETIME
SELECT @finaldate = DATEADD('d', -10, GETDATE())

SELECT p.*
FROM
(
    SELECT p1.*, ROW_NUMBER() OVER(PARTITION BY p1.PatId ORDER BY p1.VisitDate DESC) RowNo
    FROM PatVisit p1
    WHERE p1.VisitDate >= @finaldate AND p1.VisitId IN(4,7,2,8,9) 
) p INNER JOIN Device d on d.PattId = p.PatId and d.SyncDate is not null
WHERE p.RowNo=1;

int[] vid2get = new int[]{4,7,2,8,9};
var qry1 = PatVisit_context
	.Where(x=> vid2get.Any(y=>y==x.VisitId));

int[] vid2get = new int[]{4,7,2,8,9};
DateTime finaldate = DateTime.Now.AddDays(-10);
var qry1 = (from pv in PatVisit_context
	.Where(x=> vid2get.Any(y=>y==x.VisitId) && x.VisitDate >= finaldate)
	.GroupBy(x=> x.PatId)
	.Select(grp=> grp.OrderByDescending(y=>y.VisitDate).First())
	join d in Device_context on pv.PatId equals d.PatId && d.SyncDate != null
	select pv)
	.ToList();

Previous PostNext Post

Related code examples