开发者

Showing Notes using String.Join

I have a textbox in which users can put their notes and then I am showing those notes on the right hand side in the same page. Now in my DB I have columns:

-UserName

-Notes

Now here is the code to get the notes using LINQ2SQL:

int getName = Int16.Parse(开发者_开发技巧Session["Selected"].ToString());
var showNotes = from r in em.Test
               where r.Name == getName
               select r.Note;
var showUser = from r in em.Test
               where r.Name == getName
               select r.UserName;

tbShowNote.Text = String.Join(Environment.NewLine, showNotes);
tbShowNote.Text = String.Join(Environment.NewLine, showUser);

This one shows me the UserName but not the Note. I want to show something like this:

  1. This is a Test Note. -UserName1


Just select notes and user name in one query, then do your formatting afterwards:

var showNotes = from r in em.Test
                where r.Name == getName
                select new { Name = r.UserName, Notes = r.Note }

var userNotes = showNotes.Select((x,i) => string.Format("{0}. {1}-{2}", 
                                                        i, 
                                                        x.Notes, 
                                                        x.Name));

tbShowNote.Text = String.Join(Environment.NewLine, userNotes );
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜