LinQ的显式问题 [英] Explicit problem with LinQ

查看:57
本文介绍了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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆