简体   繁体   English

将linq查询转换为ObservableCollection

[英]Casting a linq query to ObservableCollection

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? 我在执行约束力吗?

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM