Change sql to linq
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!
精彩评论