复杂的 LINQ-TO-SQL 解决方案......或者只是我的经验不足?
Complex LINQ-TO-SQL solution.......or just my inexperience?
学习 LINQ-TO-SQL 并且进展顺利。但现在我认为我处于 "walk before you run" 阶段。
这个 LINQ-TO-SQL 有效:
var arr = (from up in db.fad_user_physician
join u in db.fad_user on up.userID equals u.userID
where up.physicianID.ToString() == userIdString
select new ListOfUsersForPhysician
{
userID = u.userID,
forename = u.forename,
surname = u.surname,
email = u.email,
gender = u.gender,
dobStr = u.dob.ToString()
}).ToList();
我现在想在其中加入一个计算。
因此 TSQL (有效)是:
(注意:physicianID 是我唯一需要使用的东西,从他们的登录详细信息中获得)
2016 年 2 月 17 日编辑16:03:
select fuf.userid,
((CAST(COUNT(fuf.userid)AS DECIMAL(6,2))/
(DATEDIFF(dd,fu.dateJoined,GETDATE())*5))*100) AS 'percent'
from fad_user_physician fup
inner join fad_user fu on fu.userID = fup.userID
inner join fad_userFoods fuf on fuf.userID = fu.userID
inner join fad_food ff on ff.foodID = fuf.FoodID
where fup.physicianID = '5C46F531-FF64-4254-8072-F291627ABD3D'
AND fuf.quantityAmount >= ff.portionSize
group by fuf.userID,fu.dateJoined
所以基本上我想要一个医生的用户列表,以及百分比计算。
我在谷歌上搜索了很多小时,包括教程和所有内容。但只是陷入了 LINQ-TO-SQL 的复杂性(对于新手!)
我应该使用 LET 语句吗?
我应该在 select 部分使用 LINQ-TO-SQL 语句吗?
我试过了:
let maxPos = DbFunctions.DiffDays(u.dateJoined, DateTime.Now)*5
let temp = (from fuf in db.fad_userFoods
join ff in db.fad_food on fuf.foodID equals ff.foodID
join fu in db.fad_user on fuf.userID equals fu.userID
where fuf.userID.ToString() == userIdString
&& fuf.quantityAmount >= ff.portionSize
group fuf by new {num = fuf.userID} into e
select new {total = e.Key.num}).Count()
maxpos 给了我正确的值。
但是 temp 给了我 0.
如有任何指导,我们将不胜感激!
提前致谢。
您应该能够使用导航属性执行以下操作
var results = from fu in db.fad_user
from fuf in fu.fad_userFoods
from ff in fuf.fad_foods
where fuf.userId = someGuid && fuf.quantityAmount >= ff.portionSize
group fu.userid by new {fuf.userID, fu.dateJoined} into g
select new
{
g.Key.userid,
Percent = 100 * ((decimal)(g.Count())
/ (DbFunctions.DiffDays(g.Key.dateJoined, DateTime.Now) * 5))
};
或者如果您没有导航属性,则使用 join
var results = from fu in db.fad_user
join fuf in db.fad_userFoods on fuf.userID equals fu.userID
join ff in db.fad_food on fuf.foodID equals ff.foodID
where fuf.userId = someGuid && fuf.quantityAmount >= ff.portionSize
group fu.userid by new {fuf.userID, fu.dateJoined} into g
select new
{
g.Key.userid,
Percent = 100 * ((decimal)(g.Count())
/ (DbFunctions.DiffDays(g.Key.dateJoined, DateTime.Now) * 5))
};
学习 LINQ-TO-SQL 并且进展顺利。但现在我认为我处于 "walk before you run" 阶段。
这个 LINQ-TO-SQL 有效:
var arr = (from up in db.fad_user_physician
join u in db.fad_user on up.userID equals u.userID
where up.physicianID.ToString() == userIdString
select new ListOfUsersForPhysician
{
userID = u.userID,
forename = u.forename,
surname = u.surname,
email = u.email,
gender = u.gender,
dobStr = u.dob.ToString()
}).ToList();
我现在想在其中加入一个计算。 因此 TSQL (有效)是:
(注意:physicianID 是我唯一需要使用的东西,从他们的登录详细信息中获得)
2016 年 2 月 17 日编辑16:03:
select fuf.userid,
((CAST(COUNT(fuf.userid)AS DECIMAL(6,2))/
(DATEDIFF(dd,fu.dateJoined,GETDATE())*5))*100) AS 'percent'
from fad_user_physician fup
inner join fad_user fu on fu.userID = fup.userID
inner join fad_userFoods fuf on fuf.userID = fu.userID
inner join fad_food ff on ff.foodID = fuf.FoodID
where fup.physicianID = '5C46F531-FF64-4254-8072-F291627ABD3D'
AND fuf.quantityAmount >= ff.portionSize
group by fuf.userID,fu.dateJoined
所以基本上我想要一个医生的用户列表,以及百分比计算。
我在谷歌上搜索了很多小时,包括教程和所有内容。但只是陷入了 LINQ-TO-SQL 的复杂性(对于新手!)
我应该使用 LET 语句吗? 我应该在 select 部分使用 LINQ-TO-SQL 语句吗? 我试过了:
let maxPos = DbFunctions.DiffDays(u.dateJoined, DateTime.Now)*5
let temp = (from fuf in db.fad_userFoods
join ff in db.fad_food on fuf.foodID equals ff.foodID
join fu in db.fad_user on fuf.userID equals fu.userID
where fuf.userID.ToString() == userIdString
&& fuf.quantityAmount >= ff.portionSize
group fuf by new {num = fuf.userID} into e
select new {total = e.Key.num}).Count()
maxpos 给了我正确的值。 但是 temp 给了我 0.
如有任何指导,我们将不胜感激! 提前致谢。
您应该能够使用导航属性执行以下操作
var results = from fu in db.fad_user
from fuf in fu.fad_userFoods
from ff in fuf.fad_foods
where fuf.userId = someGuid && fuf.quantityAmount >= ff.portionSize
group fu.userid by new {fuf.userID, fu.dateJoined} into g
select new
{
g.Key.userid,
Percent = 100 * ((decimal)(g.Count())
/ (DbFunctions.DiffDays(g.Key.dateJoined, DateTime.Now) * 5))
};
或者如果您没有导航属性,则使用 join
var results = from fu in db.fad_user
join fuf in db.fad_userFoods on fuf.userID equals fu.userID
join ff in db.fad_food on fuf.foodID equals ff.foodID
where fuf.userId = someGuid && fuf.quantityAmount >= ff.portionSize
group fu.userid by new {fuf.userID, fu.dateJoined} into g
select new
{
g.Key.userid,
Percent = 100 * ((decimal)(g.Count())
/ (DbFunctions.DiffDays(g.Key.dateJoined, DateTime.Now) * 5))
};