将linq查询转换为ObservableCollection

Ero*_*ocM 2 c# linq wpf observablecollection

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

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);
Run Code Online (Sandbox Code Playgroud)

这是我的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));
        }
      }

    }

  }
Run Code Online (Sandbox Code Playgroud)

我在修复绑定方面遇到了问题.这条线特别是:

var x = new ObservableCollection<Source>(query);
Run Code Online (Sandbox Code Playgroud)

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

我正在做绑定吗?

gle*_*eng 6

SourceItem因此您应该使用以下类型:

new ObservableCollection<SourceItem>(query.ToList());
Run Code Online (Sandbox Code Playgroud)

  • @ErocM:你的例子中的ObservableCollection有**错误的**泛型类型,`Source` - 你的查询返回`SourceItem`.不需要`ToList()`,你需要修复你的类型. (4认同)