c# - 将 linq 查询转换为 ObservableCollection

标签 c# wpf linq observablecollection

这是我尝试将数据网格绑定(bind)到的代码:

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

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

    }

  }

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

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

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

我的绑定(bind)正确吗?

最佳答案

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

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

关于c# - 将 linq 查询转换为 ObservableCollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21911207/

相关文章:

c# - C#中的DES加密

c# - 如何展平包含相关类的 IEnumerable 的类

c# - 如何从 ASP.Net 和 C#.Net 代码生成流程图?

c# - 使用 LINQ 选择具有不同列值的行

c# - SQL "IN"CLAUSE 性能问题

c# - 如何在标题栏中显示带有下标或上标的文本?

c# - 以编程方式调用 WPF TargetedTriggerAction

c# - wpf/mvvm - 如何制作一个组合框,从 mvvm 中的选定项目中设置特定值

.net - LINQ(语言集成查询)的优缺点

c# - Entity Framework - 保存父实体时未将新子实体添加到数据库(仅在生产环境中)