【问题标题】:c# Find missing sequence number on object using linqc# 使用 linq 查找对象上缺失的序列号
【发布时间】:2018-01-27 18:57:21
【问题描述】:

我有一个包含一个类的列表,并且有一个名为TransactNo 的整数属性。

在另一个帖子中发现了这个

var list = new List<int>(new[] { 1, 2, 4, 7, 9 }); var result = Enumerable.Range(0, 10).Except(list);

如何在数组或对象列表中使用它?

var list<myclass> obj = new list<myclass>();
var result = Enumerable.Range(obj.transactNo[0],obj.transactNo[100]).Except(obj);  

我遇到一个错误

严重性代码描述项目文件行抑制状态 错误 CS1929 'IEnumerable&lt;int&gt;' 不包含 'Except' 的定义,并且最佳扩展方法重载 'Queryable.Except&lt;&lt;anonymous type: int TransNo&gt;&gt;(IQueryable&lt;&lt;anonymous type: int TransNo&gt;&gt;, IEnumerable&lt;&lt;anonymous type: int TransNo&gt;&gt;)' 需要类型为 'IQueryable&lt;&lt;anonymous type: int TransNo&gt;&gt;' 的接收器

public class myclass
{
    public int TransactNo { get; set; }
    public string Description { get; set; }
    public decimal Amount { get; set; }
}

【问题讨论】:

  • 你的myclass怎么样?
  • 你添加using System.Linq了吗?
  • 当您提供伪代码时,很难知道您的 实际 问题是什么 - var list&lt;myclass&gt; obj = new list&lt;myclass&gt;(); 一开始就不起作用,因为 C# 区分大小写并且类是List&lt;T&gt; 而不是list&lt;T&gt;。请提供minimal reproducible example
  • public class myclass { public int TransactNo { get; set; } public string Description { get; set; } public decimal Amount { get; set; } }
  • 不,不要添加评论。 在问题中添加minimal reproducible example。不仅仅是myclass 类——我们可以尝试编译它。到目前为止,您只提供了 sn-ps。

标签: c# linq


【解决方案1】:

这个呢?

var result = Enumerable.Range(obj[0].TransactNo, obj[100].TransactNo)
.Except(obj.Select(x=> x.TransactNo)); 

.Except 方法需要整数列表,而不是对象列表。这就是你出错的原因。

希望有帮助,

【讨论】:

    猜你喜欢
    • 2010-11-06
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多