【问题标题】:How to select the data from datatable which not in an IEnumerable如何从不在 IEnumerable 中的数据表中选择数据
【发布时间】:2011-12-21 07:59:09
【问题描述】:

我有一个 DataTable dbcrs,我只想获取以下可枚举中的数据:

IEnumerable<Crs> res

注意:两者的键都是id

【问题讨论】:

    标签: c# asp.net linq datatable ienumerable


    【解决方案1】:

    这是我的建议:

    var result = dbcrs.Where(item => res.FirstOrDefault(resItem => resItem.Id == item.Id) == null);
    

    【讨论】:

    • dbcrs.Where(item =&gt; res.FirstOrDefault(resItem =&gt; resItem.Id.TrimEnd() == item.[0].ToString().TrimEnd()) == null);
    【解决方案2】:

    首先您需要使用AsEnumerable() 来查询DataTable 的Rows 集合,然后使用!Contains 而不是这样:

    var query = from r in dbcrs.AsEnumerable()
            where !( from s in res select r.Id)
                   .Contains(r.Id)
            select r;
    

    【讨论】:

      【解决方案3】:

      Except 和 IEquatable 的示例

      这种方式的一个好处是您可以定义“等于”的含义,以便仍然可以使用两个可能具有相同 ID 但不相等的列表。

      例如您从两个表中获取数据,因此 Id 可以重复,但其他一些属性定义它们是否实际上相等。

      class Crs:IEquatable<Crs>
              {
                  public int Id { get; set; }
                  public string Description { get; set; }
      
                  public bool Equals(Crs other)
                  {
                      if (Object.ReferenceEquals(other, null)) 
                          return false;
      
                      if (Object.ReferenceEquals(this, other)) 
                          return true;
      
                      return Id.Equals(other.Id) && Description.Equals(other.Description);
                  }
      
                  public override int GetHashCode()
                  {
                      int hashId = Id.GetHashCode();
                      int hashDescription = Description == null ? 0 : Description.GetHashCode();
                      return hashId ^ hashDescription;
                  }
      
              }
      
              internal static void RunMe()
              {
                  var dataTable = new List<Crs>(){
                      new Crs{Id=1, Description="First"},
                      new Crs{Id=2, Description="Second"},
                      new Crs{Id=5, Description="Fifth"}
                  };
      
                  var enumerable = new List<Crs>(){
                      new Crs{Id=2, Description="Second"},
                      new Crs{Id=4, Description="Fourth"}
                  };
      
                  var distinct = dataTable.Except(enumerable);
      
                  distinct.ToList().ForEach(d => Console.WriteLine("{0}: {1}", d.Id, d.Description));
              }
      

      【讨论】:

        【解决方案4】:
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[]
                {
                    new DataColumn("Id", typeof(System.Int32)),
                    new DataColumn("Name", typeof(System.String))
        
                });
        
                dt.Rows.Add (new Object[]{1,"Test"});
                dt.Rows.Add(new Object[] {2, "Test" });
        
        
        
                var l = new Int32[] {  2, 4 };
        
        
                var l1 = dt.AsEnumerable().Where(p1 => Array.IndexOf(l, p1.Field<Int32>(0))<0).CopyToDataTable();
        

        这将返回一行,因为在 Datatable 和数组中都有一个共同的值,即 2。所以输出将是

        2、测试

        【讨论】:

          猜你喜欢
          • 2016-08-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-06
          • 2019-03-04
          • 1970-01-01
          • 2022-01-16
          • 1970-01-01
          相关资源
          最近更新 更多