开发者

How to store int value in the table column with comma seperated using asp.net mvc

I have a table called Comments. with each comment I have comment Id,.

on my user screen I can add more than One Comment to each user.. so that comment id needs to store for this use as 1,2,3,.. Etc.. that means I added 1 2 3 comment Ids for the user..

How to store those comma seperated values in to the table column?开发者_运维百科

Thanks


Don't. Use a join table that has columns for UserID and CommentID to link the user with their comments. Then you can get the set of comments for a user with a three-way join between the User, UserComments, and Comments tables.

select Comments.*
from Users inner join UserComments on Users.UserID == UserComments.UserID
           inner join Comments on UserComments.CommentID == Comments.CommentID
where Users.UserID == @userid

where @userid is the id of the user in question.

With LINQ you simply need to reference the proper entity sets and, probably, use SelectMany.

var comments = context.Users
                      .Where( u => u.UserID == userid )
                      .SelectMany( u => u.UserComments
                                         .SelectMany( uc => uc.Comments ) );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜