问题
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();
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 .
回答1:
Try this,
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();
Note: change your profile id type accordingly here I have assumed to have as string.
来源:https://stackoverflow.com/questions/26480250/mvc4-take-value-from-a-tolist-query-and-apply-it-to-another-query