c# - Linq query from IEnumerable of integers -
i have logical problem within asp.net mvc
project. i'm developing social media app looks facebook. got work except 1 thing.
the application has table called follow
, contains loggedinuser_id
, usertofollow_id
. have ienumerable
of integers contains list of userstofollow_id
logged in user, method looks like:
public ienumerable<post> postsforflow(int userid, string username) { ienumerable<int> usertofollowid = db.follow.where(a => a.loggedinuser.id == userid).select(b => b.usertofollow.id); return null; }
how foreach on integers of posts loggedinuser want follow?
can resolve linq query?
would work? assuming user
has 1 many relationship posts
:
public ienumerable<post> postsforflow(int userid) { var useridstofollow = db.follow.where(a => a.loggedinuser.id == userid).select(b => b.usertofollow.id); return db.users .where(u => useridstofollow.contains(u.id)) .selectmany(u => u.posts); }
edit
in order test this, since don't have database, made dummy classes mock yours. if these similar have, should work.
the user
table, has id
, list of post
public class user { public int id { get; set; } public list<post> posts { get; set; } }
the post
table, has text
property (for testing)
public class post { public string text { get; set; } }
the follow
table, has loggedinuser , usertofollow
public class follow { public user loggedinuser { get; set; } public user usertofollow { get; set; } }
the 'db', contains list of user
, list of follow
(these tables)
public static class db { public static list<follow> follow = new list<follow>(); public static list<user> users = new list<user>(); }
then created method populate database 10 users. user id '0' "logged in user", , users 1 - 9
being followed user 0
, , have 4 posts each
/// <summary> /// populates db 10 users. users /// 1 9 being followed user 0 /// </summary> public static void populatedb() { // create logged in user var loggedinuser = new user { id = 0, posts = new list<post>() }; db.users.add(loggedinuser); // create 9 other users 4 posts each (int = 1; < 10; i++) { var newuser = new user { id = i, posts = new list<post> { new post {text = "post #" + i}, new post {text = "post #" + (i * 10)}, new post {text = "post #" + (i * 20)}, new post {text = "post #" + (i * 30)} } }; // add other user db.users.add(newuser); // have logged in user follow new user db.follow.add(new follow { loggedinuser = loggedinuser, usertofollow = newuser }); } }
then, test out, call 2 methods:
public static void main() { populatedb(); var postsforflow = postsforflow(0); // postsforflow has 36 entries - 4 each // of 9 users user '0' following console.writeline(postsforflow.count()); // output: 36 (9 users * 4 posts each) // now, test further, stop following users have ids // leave 5 users: 1, 3, 5, 7, 9 foreach (var follow in db.follow .where(f => f.usertofollow.id % 2 == 0) .tolist()) { db.follow.remove(follow); } postsforflow = postsforflow(0); console.writeline(postsforflow.count()); // output: 20 (5 users * 4 posts each) }
Comments
Post a Comment