我的code是非常inneficient,心中考虑看看这个简单的LINQ的使用? [英] My code is very inneficient, mind taking a look for this simple Linq usage?

查看:128
本文介绍了我的code是非常inneficient,心中考虑看看这个简单的LINQ的使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是应该从一个XML响应分析信息并返回用户的集合下面的方法。

我已经选择创建一个朋友类和一个列表返回给调用方法。

下面是我有这么远,但我注意到,ids.ToList()。计数方法解析每一个ID元素列表,那么它再次在有条件的。这只是超级无效。我需要一些帮助解决这一点,因为我没那么伟大的LINQ的。谢谢!

 公开名单<朋友>找朋友()
        {
            名单<朋友>好友列表=新名单,其中;朋友>();

            VAR的朋友= doc.Element(平局)元素(配置文件)元素(朋友)的元素(用户)。;
            VAR IDS的FR的朋友=
                      选择fr.Element(ID)值。

            变种的名称由FR的朋友=
                        选择fr.Element(name)的价值。

            VAR的网址从FR的朋友=
                        选择fr.Element(URL)的价值。

            VAR照片来自FR的朋友=
                        选择fr.Element(图片)值。

            如果(ids.ToList()计数> 0)
            {
                的for(int i = 0; I< ids.ToList()计数;我++)
                {
                    朋友哥们=新朋友();
                    buddy.ID = ids.ToList()[我]
                    buddy.Name = names.ToList()[我]
                    buddy.URL = urls.ToList()[我]
                    buddy.Photo = photos.ToList()[我]

                    friendList.Add(好友);
                }
            }

            返回好友列表;
        }
 

解决方案

第一个问题 - 你必须返回一个名单,其中;朋友> ?你可以返回一个的IEnumerable<朋友> 呢?如果是的话,表现得好多了:

 的IEnumerable<朋友>找朋友()
{
    返回doc.Descendants(用户)选择(USER =>新的朋友{
        ID = user.Element(ID),价值,
        名称= user.Element(名),价值,
        URL = user.Element(URL),价值,
        照片= user.Element(照片)。值
    });
}
 

而不是实际创建新的水桶和填充值放进去,这将创建一个投影,或者一个新的对象,仅仅包含了所有的逻辑进行的如何的创建新的朋友对象,而无需实际创建它们。当呼叫者最终开始的foreach 过了IEnumerable他们得到建立。这就是所谓的延迟执行

这也使得一个假设 - 所有的<使用者> 在你的XML片段的节点是朋友。如果事实并非如此,在XML选择的第一部分可能需要一个的的要复杂得多。

和作为@anon所指出的,即使你需要返回一个名单,其中;朋友> 由于某种原因,从你提供的,你可以在信息不明显只需拨打 .ToList()在return语句的结束。这将只是执行我上面直接进入一个新的水桶投影,所以你永远只能创建一个。

I have the following method that is supposed to parse information from an XML response and return a collection of users.

I've opted into creating a Friend class and returning a List to the calling method.

Here's what I have so far, but I noticed that the ids.ToList().Count method parses every single id element to a List, then does it again in the for conditional. It's just super ineffective. I need some help solving this because I'm not that great with Linq. Thank you!

        public List<Friend> FindFriends()
        {
            List<Friend> friendList = new List<Friend>();

            var friends = doc.Element("ipb").Element("profile").Element("friends").Elements("user");
            var ids = from fr in friends
                      select fr.Element("id").Value;

            var names = from fr in friends
                        select fr.Element("name").Value;

            var urls = from fr in friends
                        select fr.Element("url").Value;

            var photos = from fr in friends
                        select fr.Element("photo").Value;

            if (ids.ToList().Count > 0)
            {
                for (int i = 0; i < ids.ToList().Count; i++)
                {
                    Friend buddy = new Friend();
                    buddy.ID = ids.ToList()[i];
                    buddy.Name = names.ToList()[i];
                    buddy.URL = urls.ToList()[i];
                    buddy.Photo = photos.ToList()[i];

                    friendList.Add(buddy);
                }
            }            

            return friendList;
        }

解决方案

First question - do you have to return a List<Friend>? Can you return an IEnumerable<Friend> instead? If so, performance gets a lot better:

IEnumerable<Friend> FindFriends()
{
    return doc.Descendants("user").Select(user => new Friend {
        ID = user.Element("id").Value,
        Name = user.Element("name").Value,
        Url = user.Element("url").Value,
        Photo = user.Element("photo").Value
    });
}

Rather than actually creating new buckets and stuffing values into them, this creates a projection, or a new object that simply contains all of the logic for how to create the new Friend objects without actually creating them. They get created when the caller eventually starts to foreach over the IEnumerable. This is called "deferred execution".

This also makes one assumption - All the <user> nodes in your XML fragment are friends. If that isn't true, the first part of the XML selection might need to be a little more complex.

And as @anon points out, even if you do need to return a List<Friend> for some reason not obvious from the information you've provided, you can just call .ToList() at the end of the return statement. This will just execute the projection I described above straight into a new bucket, so you only ever create one.

这篇关于我的code是非常inneficient,心中考虑看看这个简单的LINQ的使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆