Explicit problem with LinQ
A followup to this previous question:
Current code:
var query = from b in books
select new
{
Title = b.Title,
StockAvailable = bookexamples.Count(be =>
be.BookID == b.BookID &&
be.OrderDetailID == null
)
};
Goal:
Replacequery
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:Error 1 Cannot implicitly convert type
'System.Collections.Generic.IEnumerable<AnonymousType#1>'
to'System.Collections.Generic.IEnumerable<BokButik1.Models.test>'
. An expli开发者_C百科cit conversion exists (are you missing a cast?)
Question:
How should I solve this problem?// Fullmetalboy
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
)
};
You'll need to adjust StockAvailable
to represent an int
as Count
returns an int.
Also, note, that class names in C# are written with a capital letter at the beginning.
精彩评论