使用Linq将对象添加到List

时间:2018-01-10 22:28:22

标签: c# linq

我试图通过使用谷歌地图API执行一些操作来向另一个对象添加N个对象列表,但它只显示对象中的最后一个值。

这是我的代码

if (Display.Dispaylist.Count > 0)
{
    foreach (var d in Display.Dispaylist)
    {
        requestUri = string.Format("https://maps.googleapis.com/maps/api/place/details/xml?placeid=" + d.Placeid + "&radius=7500&sensor=true&key=AIzaSyA0SrtzNyotUjgqI8cwbfYNrRUkdCoACd8");
        WebRequest request2 = WebRequest.Create(requestUri);
        WebResponse response2 = request2.GetResponse();
        XDocument xdoc2 = XDocument.Load(response2.GetResponseStream());
        XElement generalElement1 = xdoc2.Element("PlaceSearchResponse");
        Displayinfo.Dispaylisted = (from e in xdoc2.Descendants("result")
                                    select new DisplacedModelList()
                                    {
                                        Name = Convert.ToString(e.Element("name").Value),
                                        Address = (e.Element("formatted_address") != null ? Convert.ToString(e.Element("formatted_address").Value) : null),
                                        Type = keyword,
                                        PhoneNo = (e.Element("international_phone_number") != null ? Convert.ToString(e.Element("international_phone_number").Value) : null),
                                        WebSite = (e.Element("website") != null ? Convert.ToString(e.Element("website").Value) : null),
                                        Rating = (e.Element("rating") != null ? Convert.ToString(e.Element("rating").Value) : null)
                                    }).ToList<DisplacedModelList>();
        //Displayinfo.Dispaylisted.AddRange();
        //.AddRange(Displayinfo);
        //Displayinfo.Dispaylisted.AddRange(Displayinfo);                                               
    } 
}
return View(Displayinfo);

因为每次我创建新对象所以在迭代结束时它会显示最后一个迭代值。 如何获取整个列表我是LINQ的新手.. 谢谢你的帮助。

2 个答案:

答案 0 :(得分:0)

在将Display.Displaylisted属性设置为使用linq生成的新列表的代码行中。问题是您为foreach循环的每次迭代将其设置为新列表。您需要检查列表是否为null,如果它是新的,如果不是,则AddRange传入LINQ生成列表。我正在打电话,所以如果你需要一个例子我可以把我的笔记本电脑拿出去,输入代码就会很痛苦让我知道

----编辑---添加一些代码作为请求

下面我添加了一条评论,以显示您的代码出错的地方,并希望能够清除您正在做的事情

if (Display.Dispaylist.Count > 0)
    {
        foreach (var d in Display.Dispaylist)
        {
            requestUri = string.Format("https://maps.googleapis.com/maps/api/place/details/xml?placeid=" + d.Placeid + "&radius=7500&sensor=true&key=AIzaSyA0SrtzNyotUjgqI8cwbfYNrRUkdCoACd8");
            WebRequest request2 = WebRequest.Create(requestUri);
            WebResponse response2 = request2.GetResponse();
            XDocument xdoc2 = XDocument.Load(response2.GetResponseStream());
            XElement generalElement1 = xdoc2.Element("PlaceSearchResponse");
            //----------------Here you are assigning a new list to the Property for every single iteration of the foreach loop, hence why you only see the last iteration value-----/
            Displayinfo.Dispaylisted = (from e in xdoc2.Descendants("result")
                                        select new DisplacedModelList()
                                        {
                                            Name = Convert.ToString(e.Element("name").Value),
                                            Address = (e.Element("formatted_address") != null ? Convert.ToString(e.Element("formatted_address").Value) : null),
                                            Type = keyword,
                                            PhoneNo = (e.Element("international_phone_number") != null ? Convert.ToString(e.Element("international_phone_number").Value) : null),
                                            WebSite = (e.Element("website") != null ? Convert.ToString(e.Element("website").Value) : null),
                                            Rating = (e.Element("rating") != null ? Convert.ToString(e.Element("rating").Value) : null)
                                        }).ToList<DisplacedModelList>();

        } 
    }
    return View(Displayinfo);

以下是我认为它应该做的事情

if (Display.Dispaylist.Count > 0)
        {
            //Initialise the list outside of the loop, just do it once 
            Displayinfo.Dispaylisted = new List<DisplacedModelList>();
            foreach (var d in Display.Dispaylist)
            {
                requestUri = string.Format("https://maps.googleapis.com/maps/api/place/details/xml?placeid=" + d.Placeid + "&radius=7500&sensor=true&key=AIzaSyA0SrtzNyotUjgqI8cwbfYNrRUkdCoACd8");
                WebRequest request2 = WebRequest.Create(requestUri);
                WebResponse response2 = request2.GetResponse();
                XDocument xdoc2 = XDocument.Load(response2.GetResponseStream());
                XElement generalElement1 = xdoc2.Element("PlaceSearchResponse");
                Displayinfo.Dispaylisted.AddRange(
                    (from e in xdoc2.Descendants("result")
                     select new DisplacedModelList()
                     {
                         Name = Convert.ToString(e.Element("name").Value),
                         Address = (e.Element("formatted_address") != null ? Convert.ToString(e.Element("formatted_address").Value) : null),
                         Type = keyword,
                         PhoneNo = (e.Element("international_phone_number") != null ? Convert.ToString(e.Element("international_phone_number").Value) : null),
                         WebSite = (e.Element("website") != null ? Convert.ToString(e.Element("website").Value) : null),
                         Rating = (e.Element("rating") != null ? Convert.ToString(e.Element("rating").Value) : null)
                     }).ToList<DisplacedModelList>()
                );

            }
        }
        return View(Displayinfo);

答案 1 :(得分:0)

使用原始代码,首先需要在IO::File之前初始化print $self ...

public class Xbonacci
{
    public double[] Tribonacci(double[] signature, int n)
    {
        double[] r = { 0 };
        if (n==0)
        {
            return r;
        }

        List<double> t = new List<double>();

        for (int i = 0; i < signature.Length; i++)
        {
            t.Add(signature[i]);
        }

        for (int i = 1; i < n-2; i++)
        {
            t.Add(t[i]+t[i+1]+t[i-1]);    
        }

        return t.ToArray(); 
    }
}

然后,在List内添加LINQ查询的所有返回值:

foreach

如果您确实想使用LINQ来分配DisplayInfo.DisplayListed = new List<DisplacedModelList>(); ,则需要在查询中包含foreach。您可以使用以下内容替换DisplayInfo.DisplayListed.AddRange((from e in xdoc2.Descendants("result") select new DisplacedModelList() { Name = Convert.ToString(e.Element("name").Value), Address = (e.Element("formatted_address") != null ? Convert.ToString(e.Element("formatted_address").Value) : null), Type = keyword, PhoneNo = (e.Element("international_phone_number") != null ? Convert.ToString(e.Element("international_phone_number").Value) : null), WebSite = (e.Element("website") != null ? Convert.ToString(e.Element("website").Value) : null), Rating = (e.Element("rating") != null ? Convert.ToString(e.Element("rating").Value) : null) }).ToList()); 正文中的所有内容:

List