wpf 将 linq 查询转换为 ObservableCollection

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21911207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:56:41  来源:igfitidea点击:

Casting a linq query to ObservableCollection

c#wpflinqobservablecollection

提问by ErocM

This is my code that I am trying to bind a datagrid to:

这是我试图将数据网格绑定到的代码:

var query = (from s in entity.Sources 
                  where s.CorporationId == corporationId 
                  select new SourceItem
                  {
                    CorporationId =  s.CorporationId,
                    Description=s.Description,
                    IsActive =  s.IsActive,
                    Name=s.Name,
                    SourceId=s.SourceId,
                    TokenId=s.TokenId
                  });
      var x = new ObservableCollection<Source>(query);

And this is my SourceItetm class:

这是我的 SourceItetm 类:

   private void SourceDataGrid_AddingNewItem(object sender, System.Windows.Controls.AddingNewItemEventArgs e)
    {
      var sources = new Source();
      sources.CorporationId = _corporationId;
      sources.Description = string.Empty;
      sources.IsActive = true;
      sources.Name = string.Empty;
      sources.SourceId = Guid.NewGuid();
      sources.TokenId = Guid.NewGuid();
      e.NewItem = sources;
    }

    public class SourceItem
    {
      private Guid _corporationId1;
      private string _description;
      private bool _isActive;
      private string _name;
      private Guid _sourceId;
      private Guid _tokenId;

      public Guid CorporationId
      {
        set
        {
          _corporationId1 = value;
          onPropertyChanged(this, "CorporationId");
        }
        get { return _corporationId1; }
      }

      public string Description
      {
        set
        {
          _description = value;
          onPropertyChanged(this, "Description");
        }
        get { return _description; }
      }

      public bool IsActive
      {
        set
        {
          _isActive = value;
          onPropertyChanged(this, "IsActive");
        }
        get { return _isActive; }
      }

      public string Name
      {
        set
        {
          _name = value;
          onPropertyChanged(this, "NAme");
        }
        get { return _name; }
      }

      public Guid SourceId
      {
        set
        {
          _sourceId = value;
          onPropertyChanged(this, "SourceId");
        }
        get { return _sourceId; }
      }

      public Guid TokenId
      {
        set
        {
          _tokenId = value;
          onPropertyChanged(this, "TokenId");
        }
        get { return _tokenId; }
      }


      // Declare the PropertyChanged event
      public event PropertyChangedEventHandler PropertyChanged;

      // OnPropertyChanged will raise the PropertyChanged event passing the
      // source property that is being updated.
      private void onPropertyChanged(object sender, string propertyName)
      {
        if (PropertyChanged != null)
        {
          PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
        }
      }

    }

  }

I'm having problems getting the binding right. This line in particular:

我在正确绑定时遇到问题。特别是这一行:

var x = new ObservableCollection<Source>(query);

It is telling me that it cannot resolve constructor.

它告诉我它无法解析构造函数。

Am I doing the binding right?

我做的绑定正确吗?

回答by gleng

The type you select is SourceItemtherefore you should use:

您选择的类型SourceItem因此您应该使用:

new ObservableCollection<SourceItem>(query.ToList());