开发者

Change sql to linq

开发者 https://www.devze.com 2023-02-09 18:14 出处:网络
hi could anyone advice how to change the following sql script to linq? SELECT *, CASE开发者_Python百科 WHENdatepart(day, DateIssue)<27 THEN datepart(month,DateIssue)

hi could anyone advice how to change the following sql script to linq?

SELECT *, CASE开发者_Python百科 WHEN datepart(day, DateIssue)<27 THEN datepart(month,DateIssue) ELSE datepart(month,DateIssue) % 12 + 1 END as group_name FROM Payments;

thanks


Assuming you have a list of payments called Payments:

var query = from p in Payments
            let dateIssue = (DateTime)p["DateIssue"]
            let gName = (dateIssue.Day < 27) ? (dateIssue.Month) : (dateIssue.Month % 12 + 1)
            select new { payment = p, groupName = gName };

The Payments list can be anything that derives from Iterable

if needed you can iterate that this way:

foreach (var value in query) {
     // value.payment the original item of Payments
     // and value.groupName contains the other column
}

God bless!

0

精彩评论

暂无评论...
验证码 换一张
取 消