简体   繁体   中英

Convert SQL to LINQ in c#

I am unable to write the below mentioned query into Linq.

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

I am able to execute in SQL but unable to convert into linq. More I used linqer to convert but it also not working.

SQL language is limited and often you have to use kludges get required results. c# is a much more robust language. Here is my model of the query

    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; }

    }

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