开发者

Showing Notes using String.Join

开发者 https://www.devze.com 2023-03-05 09:25 出处:网络
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:

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

精彩评论

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

关注公众号