mvc4如何从控制器中的tolist查询中提取值 [英] mvc4 How to extract value from a tolist query in controller
问题描述
好的,这是基本设置;一个用户登录后,我获得了该用户 myid 的唯一ID,然后我尝试通过进入变量 myfriends ,请注意,我只选择了代表朋友唯一ID的friendID列.然后,将myid与profileID进行比较,如果他们是朋友,则ID应该匹配.问题是我在 myfriends 变量上使用了. tolist(),而我似乎无法提取friendID(我使用的是tolist(),因为用户可以有1个以上的朋友,但tolist仅计算记录的数量)以在 friendprofile 变量中进行比较,因为.tolist()仅提供提取多少记录的值.如何从我的朋友那里获取friendID值,以便可以在friendprofile中进行比较,以便向用户显示朋友的profile?任何帮助都很好
Ok so this is the basic setup; A user logins I get the unique ID of that user myid, I then try to get a list of that logged in user friends by going into the friends table which is shown in the variable myfriends notice that I have only selected the friendID column which represents the uniqueID of a friend. I then compare the myid to the profileID which should match if they're friends. The problem is that I am using a .tolist() on the myfriends variable and I can't seem to extract the friendID (I am using the tolist() because a user can have more than 1 friend but the tolist only counts the amount of records) to compare in the friendprofile variable because the .tolist() only gives a value of how many records are extracted. How can I get the friendID value from myfriends so that I can compare it in the friendprofile so that I can show the users friends profile ? any help would be great
[Authorize]
public ActionResult myprofile()
{
// Logged in User unique ID
int myid = Convert.ToInt32(User.Identity.Name);
// list of Logged in User friends by ID
var myfriends = sqlConnection.Query<friend>("Select friendID from friends where myid=@profileID",new { profileID = myid }).ToList();
// get friends profiles
var friendprofile = sqlConnection.Query<profile>("Select * from profiles where friendID=@profile",new { profile = myfriends }).ToList();
}
推荐答案
您可以使用 SQL IN运算符
int myid = Convert.ToInt32(User.Identity.Name);
var friendprofile = sqlConnection.Query<profile>("Select * from profiles where friendID IN (Select friendID from friends where myid=@profile)",new { profile = myid }).ToList();
这篇关于mvc4如何从控制器中的tolist查询中提取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!