加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 我想查看一个IEnumerable的Count,但效率非常低

发布时间:2020-12-16 01:36:52 所属栏目:百科 来源:网络整理
导读:特别感谢Rex M for this bit of wisdom: public IEnumerableFriend FindFriends() { //Many thanks to Rex-M for his help with this one. //https://stackoverflow.com/users/67/rex-m return doc.Descendants("user").Select(user = new Friend { ID = us
特别感谢Rex M for this bit of wisdom:

public IEnumerable<Friend> FindFriends()
    {
        //Many thanks to Rex-M for his help with this one.
        //https://stackoverflow.com/users/67/rex-m

        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
        });
    }

找到所有用户朋友后,我需要在WPF表单上显示它们.我有一个问题,并非所有用户都有至少5个朋友,有些甚至没有朋友!这就是我所拥有的:

private void showUserFriends()
    {
        if (friendsList.ToList().Count > 0)
        {
            friend1.Source = new BitmapImage(new Uri(friendsList.ToList()[0].Photo));
            label11.Content = friendsList.ToList()[0].Name;

            friend2.Source = new BitmapImage(new Uri(friendsList.ToList()[1].Photo));
            label12.Content = friendsList.ToList()[1].Name;

            //And so on,two more times. I want to show 4 friends on the window.
        }            
    }

所以这个问题有两个部分:

>您如何建议我处理用户可能拥有的不同数量的朋友.使用我当前的代码,如果用户没有朋友,我会得到IndexOutOfBounds异常,因为friendsList [0]不存在.
>如何更有效地处理用户是否有朋友的验证?调用.ToList()似乎很费劲.

解决方法

当您在IEnumerable上调用ToList()时,您正在执行的操作是枚举可枚举列表的所有元素并将结果放入容器中.所以“代码味道”是在同一个IEnumerable上多次调用ToList()的代码,它应该只执行一次并保存到变量中.

有一个简单的经验法则.如果您作为一个整体在IEnumerable列表上运行(Linq表达式)或者只是从头到尾导航列表然后使用IEnumerable,如果您需要通过索引访问列表,或计算元素数量或导航两个方向通过列表,首先创建一个List容器并使用它.

List<Friend> friends = FindFriends().ToList();
//Then use the friends list....

现在,关于列表中是否有任何内容,正如这里的几个人所提到的,你可以使用数据绑定和像ItemsControl这样的控件,但是如果你想要动态构建UI,请使用循环,不要索引数组.

List<Friend> friends = FindFriends().ToList();
if(friends.Count > 0)
{
  foreach(Friend f in friends)
  {
    //Create your Control(s) and add them to your form or panel's controls container
    // somthing like (untested) myPanel.Controls.Add(new Label(){Text = f.Name});
  }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读