【问题标题】:mvc4 How to extract value from a tolist query in controllermvc4如何从控制器中的tolist查询中提取值
【发布时间】:2014-10-20 04:13:36
【问题描述】:

好的,这是基本设置;用户登录我得到该用户的唯一 ID myid,然后我尝试通过进入变量 myfriends 请注意,我只选择了代表朋友唯一 ID 的朋友 ID 列。然后我将 myid 与 profileID 进行比较,如果他们是朋友则应该匹配。问题是我在 myfriends 变量上使用了 .tolist() 并且我似乎无法提取friendID(我正在使用 tolist() 因为用户可以有超过 1 个朋友,但 tolist 只计算记录的数量)要在 friendprofile 变量中进行比较,因为 .tolist() 只给出提取多少记录的值。如何从 myfriends 获取friendID 值,以便在friendprofile 中进行比较,以便向用户显示朋友的个人资料?任何帮助都会很棒

       [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();
   }

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4 actionresult


    【解决方案1】:

    您可以使用SQL IN Operator 进行单个查询

    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-10
      相关资源
      最近更新 更多