简体   繁体   中英

SQL to LINQ C# EntityFramework

I'm on my way to convert all my straight SQL programms to Entity Framework . So I need LINQ and LE .

But I'm not able to do this in LINQ:

SELECT 
    a.abteilung, 
    sum(d.kosten) 
FROM 
    tdaten d, 
    abteilung a 
WHERE 
    d.sourcenr = a.sourcenr AND
    d.datum between '" + string.Format("{0:yyyy-MM-dd}", DTP_from.Value) + "' 
        and '"+string.Format(format: "{0:yyyy-MM-dd}", arg0: DTP_to.Value)+"'
GROUP BY 
    a.abteilung;", _con);

Thanks

An option.
The whole can b breakdown into 3 sub-queries in LINQ.

  1. Get the Records
  2. Take the sum
  3. The group by the result.

Following are the queries.:

 var results = from a in abteilung
          join d in tdated on d.sourcenr equals a.sourcenr
          where 
          d.dateum >= fromDate && d.datum <= ToDate.AddDays(1)
          select  new {a.abteilung,  d.kosten};

    var sumOfKotsen = results.Sum(x=>x.kosten);

    var groupResult = results.GroupBy(x=>x.abteilung);

Assuming that the dateFrom and ToDate are the DateTime objects.

Something like this?

from a in abteilung 
join d in tdaten 
   on a.sourcenr equals d.sourcenr
where d.datum >= DTP_from.Value && d.datum <= DTP_to.Value
group d by a.abteilung into g
select new
{
  abteilung = g.Key,
  kosten = g.Sum(d => d.kosten)
}

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