LinQ的显式问题 [英] Explicit problem with LinQ
问题描述
当前代码:
var query = from b in books
select new
{
Title = b.Title,
StockAvailable = bookexamples.Count(be =>
be.BookID == b.BookID &&
be.OrderDetailID == null
)
};
目标:
将query
替换为应包含LINQ查询中的强类型数据的IEnumerable<test>
.
Goal:
Replace query
with an IEnumerable<test>
that should contain strongly-typed data from the LINQ query.
public class test
{
public string Title { get; set; }
public List<int> StockAvailable { get; set; }
}
问题:
收到错误消息:
Problem:
Recieve a error message:
错误1无法隐式转换类型
'System.Collections.Generic.IEnumerable<AnonymousType#1>'
到'System.Collections.Generic.IEnumerable<BokButik1.Models.test>'
. 存在显式转换(您是 缺少演员表?)
Error 1 Cannot implicitly convert type
'System.Collections.Generic.IEnumerable<AnonymousType#1>'
to'System.Collections.Generic.IEnumerable<BokButik1.Models.test>'
. An explicit conversion exists (are you missing a cast?)
问题:
我该如何解决这个问题?
Question:
How should I solve this problem?
//Fullmetalboy
// Fullmetalboy
推荐答案
您需要修改查询以返回test
个对象(当前,它正在返回自治对象).
You need to modify the query to return test
objects (currently, it is returning anomynous objects).
var query = from b in books
select new test()
{
Title = b.Title,
StockAvailable = bookexamples.Count(be =>
be.BookID == b.BookID &&
be.OrderDetailID == null
)
};
您需要调整StockAvailable
以表示int
,因为Count
返回一个整数.
You'll need to adjust StockAvailable
to represent an int
as Count
returns an int.
另外,请注意,C#中的类名开头是大写字母.
Also, note, that class names in C# are written with a capital letter at the beginning.
这篇关于LinQ的显式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!