简体   繁体   English

System.Linq到GET请求的字符串问题

[英]System.Linq to String issue with GET request

Why is it when I do this: 为什么这样做:

    private void button18_Click(object sender, EventArgs e)
    {

        string uri1 = "http://localhost:8000/Service/GetMessage/{anything}";
        string tagUri = uri1.Replace("{anything}", textBox21.Text);
        XDocument xDoc = XDocument.Load(tagUri);
        var MessageID = xDoc.Descendants("Message")
                             .Select(n => new
                             {
                                 MessageID = n.Element("MessageID").Value,
                             })
            .ToString();

        textBox1.Text = MessageID;

I get this really strange output? 我得到这个非常奇怪的输出?

System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,<>f__AnonymousType7`1[System.String]]

But yet when I change ToString to ToList and use: 但是当我将ToString更改为ToList并使用时:

dataGridView10.DataSource = MessageID;

It displays it correctly? 它显示正确吗? Really struggling to find out a way to take my incoming GET request to string? 真的很难找到一种方法来将传入的GET请求转换为字符串吗?

Your LINQ query is getting a collection by nature of the call to .Descendants . 您的LINQ查询本质上是对.Descendants的调用的.Descendants If there's only one, you can use a .Single() or .First() or .FirstOrDefault() to get the only (or First) item in the list. 如果只有一个,你可以使用.Single().First().FirstOrDefault()来获得在列表中唯一的(或第一个)项目。

Then it would be 那会是

textbox1.Text = MessageID.MessageID;

Though you more likely want to do something like this: 尽管您更可能想执行以下操作:

var MessageId = xDoc.Element("Message").Element("MessageID").Value;

But I'm somewhat guessing, as I don't know the format or content of your XML. 但是我有点猜测,因为我不知道您XML的格式或内容。

There are others here who can give you a more detailed explanation, but here's what I think is happening (expanded from my comment above). 这里还有其他人可以给您更详细的解释,但这就是我认为正在发生的事情(根据我上面的评论进行了扩展)。

By default, LINQ queries return a collection, even when there's only one result. 默认情况下,即使只有一个结果,LINQ查询也会返回一个集合。 When you call .ToString() on the that collection, you get the fully qualified name of the type of the Object. 在该集合上调用.ToString()时,将获得Object类型的完全限定名称。 See Object.ToString Method - which in this case I believe is the weird output you get. 请参阅Object.ToString方法 -在这种情况下,我认为这是您得到的怪异输出。

You're probably looking for a single result (MessageID), so you could use .FirstOrDefault() instead of .ToString() , like this: 您可能正在寻找单个结果(MessageID),因此可以使用.FirstOrDefault()而不是.ToString() ,如下所示:

var MessageID = xDoc.Descendants("Message")
                .Select(n => new
                {
                    MessageID = n.Element("MessageID").Value,
                })
                .FirstOrDefault();

Note that n.Element("MessageID").Value will return a string, which you can then assign to the TextBox. 请注意, n.Element("MessageID").Value将返回一个字符串,然后您可以将其分配给TextBox。

When you convert the query to a List, you are able to bind it to the DataGridView's DataSource because DataSource takes objects that implement one of the IList interfaces ( IList in the case of a generic list). 当您将查询转换为列表时,可以将其绑定到DataGridView的DataSource,因为DataSource会采用实现IList接口之一的对象(对于通用列表, IList )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM