- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个方法,可以使用 LINQ 对两个 DataTable 执行不匹配的查询。它生成了一个错误,通过在网上查找,我已经确定了我认为错误发生的位置,但我不知道如何修复它。
public IEnumerable<int> UnbilledAuditKeys(DataTable audits, string keyFieldName) {
var billedAudits =
from x in this.GetBilledAudits().AsEnumerable()
select new {
k = x.Field<int>(keyFieldName)
};
var allAudits =
from x in audits.AsEnumerable()
select new {
k = x.Field<int>(keyFieldName)
};
var unbilled =
from a in allAudits
join b in billedAudits on a.k equals b.k
into combined
from c in combined.DefaultIfEmpty()
where c == null
select new { // This is what's causing the error (I think)
k = a.k
};
return unbilled; // This line the compiler is rejecting
}
返回的错误是
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable<int>'. An explicit conversion exists (are you missing a cast?)
我不知道如何解决它。我尝试过将整个 LINQ 表达式转换为 IEnumerable 等显而易见的方法,但这会生成运行时异常。
任何想法将不胜感激!
编辑:
final方法:
public IEnumerable<int> UnbilledAuditKeys(DataTable rosliAudits, string keyFieldName) {
var billed = this.GetBilledAudits().AsEnumerable().Select(x => x.Field<int>(keyFieldName));
var allaudits = rosliAudits.AsEnumerable().Select(x => x.Field<int>(keyFieldName));
var unbilled = allaudits.Except(billed);
return unbilled;
}
最佳答案
简单修复:
var unbilled =
from a in allAudits
join b in billedAudits on a.k equals b.k
into combined
from c in combined.DefaultIfEmpty()
where c == null
select a.k;
此外,其他两个查询似乎不需要匿名结构,最后一个查询可以大大简化:
public IEnumerable<int> UnbilledAuditKeys(DataTable audits, string keyFieldName) {
var billedAudits =
from x in this.GetBilledAudits().AsEnumerable()
select x.Field<int>(keyFieldName);
var allAudits =
from x in audits.AsEnumerable()
select x.Field<int>(keyFieldName);
var unbilled = allAudits.Except(billedAudits); // LINQ has many useful methods like this
return unbilled;
}
关于c# - IEnumerable<AnonymousType#1> 的显式转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18514968/
我有一个声明为匿名类型的数组: var list = new[] { new {Name = "A", Age = 10}, new {Name = "B", Age = 15} }
我想提取 AnoymousType,这是我的代码: static void Main(string[] args) { Extract(new { Name = "Yoza" });
我有一个包含多个连接的查询。我的第一个查询运行完美,但是当我想在我的第二个连接中使用此结果 (IQueryable) 时,出现错误。如何在我的第二个查询中使用我的第一个结果对象? 谢谢! ISessi
我正在尝试创建一个 AnonymousType 实例,如下所示: new { Channel = g.Key.Channel, Comment = g.Key.Comment, Count = g.C
编译器似乎总是命名匿名类型,以便它们包含“AnonymousType”。 new { Name = "Sean"}.GetType().Name // Gives: <>f__AnonymousTyp
您好,序列化过程中这一行是什么意思? [XmlType(AnonymousType = true)] public class classname { } xsd.exe 为所有类生成它。我阅读了文档
你知道如何修复这个错误吗?它在这一行中显示错误“foreach(itemColl 中的 int s)” 我必须做什么? Error 1 Cannot convert type 'AnonymousTy
我有一个方法,可以使用 LINQ 对两个 DataTable 执行不匹配的查询。它生成了一个错误,通过在网上查找,我已经确定了我认为错误发生的位置,但我不知道如何修复它。 public IEnumer
我有一个方法可以使用 LINQ 对两个 DataTable 执行不匹配的查询。它生成了一个错误,通过在线查看我已经确定了我认为错误发生的位置,但我不知道如何修复它。 public IEnumerabl
这个问题在这里已经有了答案: How can I parse a JSON string that would cause illegal C# identifiers? (3 个答案) Can y
我有一个 Web API,我只是想以 XML 格式向调用客户端返回一些数据。 我不断收到以下错误: Type '<>f__AnonymousType7`9[System.String,System.
我在 S/O 上看到过类似的错误,但修复对我来说并不直接。 Cannot implicitly convert type 'System.Collections.Generic.List' to 'S
我只需要获取所有团队成员的 ID,以便我可以查询联系人表。 var teamMembersIds = (from q in _repository.GetQuery
嗨,我有一个问题,我已经研究了好几个小时,但我一直收到错误 Property or indexer 'AnonymousType#1.XP' cannot be assigned to -- it i
我正在尝试制作一个函数,将查询结果值更改为文本框中的值。我可以运行 LINQ 查询,获取值,并且读取可以完美打印...但是当我尝试将查询值更改为文本框值时,出现以下错误: Property or in
我过滤了一个 List使用 Linq 连接该列表中的两个字段并返回结果,现在需要将该列表返回给调用方法。 Linq 过滤的结果类型为 IEnumerable所以我不能返回它,因为我的方法返回类型是 L
我已经用 AnonymousType 类型的对象填充了以下列表 List someList = new List(); someList.Add(new { foo = 1 }); 我的问题是我不能让
我的 C# 项目中出现错误,这让我很头疼。错误是: Cannot implicitly convert type 'System.Linq.IQueryable' to System.Collecti
我有以下代码: public List ListAll() { List products = new List(); var db_products = db.Products
在我的 MVC应用程序有 4 个模型类。 ProductModel.cs public class ProductModel { public int ProductID { get; s
我是一名优秀的程序员,十分优秀!