mvc4从查询了ToList采取值,并将其应用到另一个查询 [英] mvc4 take value from a tolist query and apply it to another query

查看:368
本文介绍了mvc4从查询了ToList采取值,并将其应用到另一个查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上(2) .Tolist()在我的控制器查询;我希望做的是从一个特定的列中获取号码列表,并嵌入到控制器中的第二.tolist查询,这个例子应该把它清除掉。

I basically have (2) .Tolist() queries in my controller; What I would like to do is get a list of numbers from a specific column and embed it into the 2nd .tolist query in the controller, this example should clear it up

int myid = Convert.ToInt32(User.Identity.Name);
// I would like to get extract the info. from ProfileID column and use in comments tolist
var friendprofile = sqlConnection.Query<profile>("Select * from  profiles where ProfileID In (Select ProfileID from followings where me=@profile)", new { profile = myid }).ToList();
var comments = sqlConnection.Query<comment>("Select * from comments where profileID=@profile", new { profile = friendprofile}).ToList();

该变种的 friendprofile 我每次运行它​​的工作原理;它给我算的2型材这是正确的(这个用户只拥有2个朋友),现在我想(从以下其中Me = @情景模式选择提供ProfileID)并使用那些获得提供ProfileID 的int值在意见的变量值。在 friendprofile 变量获取用户的好友列表,个人资料,并提取他或她的朋友唯一的ID(提供ProfileID);和注释变量选择的用户朋友们的意见。我知道,这部分是错误的个人资料= friendprofile ;我也喜欢做这样的事情个人资料= friendprofile.followings.ProfileID 但我不能,因为它是一个了ToList任何帮助或建议将是巨大的。

The var friendprofile works each time I run it; it gives me count of 2 profiles which is correct (This user only has 2 friends) now I would like to get the int value of ProfileID (Select ProfileID from followings where me=@profile) and use those values in the comments variable . The friendprofile variable gets a list of the user's friends profile and extracts his or her friends unique ID (ProfileID); and the comment variable select the users friends comments . I do know that this portion is wrong profile = friendprofile; I would have liked to do something like this profile = friendprofile.followings.ProfileID but I can't since it is a tolist any help or suggestions would be great .

推荐答案

试试这个,

修改你的第二选择查询作为,

modify your 2nd select query as,

        string profileIdValues = string.Empty;
        foreach (string proId in profileIds)
        {
            profileIdValues = proId + ", " + profileIdValues;
        }
        // Will remove the trailing ',' from the profileIdValues.
        profileIdValues = profileIdValues.Remove(profileIdValues.Length - 2, 1);
        var comments = sqlConnection.Query<comment>("Select * from comments where profileID IN (@profile)", new { profile = profileIdValues }).ToList();

注:更改您的个人ID类型据此这里我假设有作为字符串

Note: change your profile id type accordingly here I have assumed to have as string.

这篇关于mvc4从查询了ToList采取值,并将其应用到另一个查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆