LINQ中无效的匿名类型成员声明符 [英] Invalid anonymous type member declarator in LINQ

查看:40
本文介绍了LINQ中无效的匿名类型成员声明符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体.一个是学生",另一个是主题".

I have two entities. One is "Students" while another is "Subjects".

这两个实体的详细信息如下:

The details of the two entities is something like:

students { id, name}

subjects { studentID, subjectName, passed}

其中通过"是布尔类型.

where "passed" is of boolean type.

现在,我想查询学生姓名和他可以通过的科目数:

Now I want to query the student name and count of subject that he can pass with follows:

var result = from s in db.students 
select new {s.name, s.subjects.Count(i => i.passed.Equals(true)};

但是我收到错误消息msg:无效的匿名类型成员声明符.必须使用成员分配,简单名称或成员访问权限来声明匿名类型成员.

But i get error msg:Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

我不知道该怎么解决.有人可以帮我吗?谢谢

I dun know how to solve it. Would anyone help me please? thanks

推荐答案

这意味着您需要命名无法推断的匿名类型的属性

This means that you need to name your anonymous type's properties that cannot be inferred

select new 
{
    s.name, 
    Count=s.subjects.Count(i => i.passed.Equals(true))
};

通常,属性名称足够好,但是您使用的是Count方法,因此该属性没有固有名称

Usually, the property name is good enough, however you are using the Count method, so that property has no inherent name

这篇关于LINQ中无效的匿名类型成员声明符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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