HelloWorld1
HelloWorld1

Reputation: 14108

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:
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 explicit conversion exists (are you missing a cast?)

Question:
How should I solve this problem?

// Fullmetalboy

Upvotes: 0

Views: 150

Answers (1)

Femaref
Femaref

Reputation: 61497

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.

Upvotes: 4

Related Questions