开发者

How to get text from LINQ2SQL query?

I have a web page in which I am giving USER the options of writing notes. Now when ever the web page checks that a USER is:abc then it pulls up the note from the MEMO Table.

Here is my code in Page_Load():

using (EntityMemoDataContext em = new EntityMemoDataContext())
            {
                int getEntity = Int16.Parse(Session["EntityIdSelected"].ToString());
           开发者_运维知识库     var showMemo = from r in em.EntityMemoVs_1s
                               where r.EntityID == getEntity
                               select r.Memo;

                tbShowNote.Text = String.Join(@"<br />", showMemo);
            }

tbShowNote is showing me value like this:

test<br />test1<br />test1<br />test4<br />test4

And I want it like this:

Test

Test1

Test2 ...

tbShowNote is a TextBox!


You only asked for the first memo, so that's what you got back. If you want it enumerated with each one on it's own line in html, you could do this:

    using (EntityMemoDataContext em = new EntityMemoDataContext())
    {
        int getEntity1 = Int16.Parse(Session["EntityIdSelected"].ToString());
        var showMemo = from r in em.EntityMemoVs_1s
                       where r.EntityID == getEntity1
                       select new
                       {
                           r.Memo
                       };
        tbShowNote.Text = String.Join(@"<br />", showMemo);
    }

The key takeaway is if r.Memo is of type string, then the LINQ query you executed gave you back a IQueryable<string>. It's on you to decide if you want to flatten that list later.

Edit: Equiso made a good observation in that you're actually returning an IQueryable of an anonymous type, not IQueryable<string> due to the new { ... } syntax. I'd say combine his answer with mine and run with it:

var showMemo = from r in em.EntityMemoVs_1s
               where r.EntityID == getEntity1
               select r.Memo;

tbShowNote.Text = String.Join(@"<br />", showMemo);


The problem is in the select part of your linq query, you are wrapping your results in an anonymous type, that is why when you call ToString() you see { Memo = test }. You probably want it like this:

var showMemo = from r in em.EntityMemoVs_1s
               where r.EntityID == getEntity1
               select r.Memo;

After that showMemo will contain just strings.


It looks like your showMemo is a collection and you are then just assigning the top value? If you are putting them in one string then you need to aggregate them together.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜