开发者

Explicit problem with LinQ

开发者 https://www.devze.com 2023-02-08 09:14 出处:网络
A followup to this previous question: Current code: var query = from b in books select new { Title = b.Title,

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:

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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消