Linq到实体的导航属性 [英] Linq to entities navigation properties

查看:71
本文介绍了Linq到实体的导航属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在实体框架中,我有两个表(两个实体):具有一对多关系的人员和角色. 在人员表中,我具有角色的导航属性:

In entity framework i have two table (two entities): People and Role with one to many relationship. In the People tables i have a navigation property to Role:

//People.cs
public virtual ICollection<Role> Role { get; set; }

现在,我想检索所有具有酒保"角色的人.我怎样才能做到这一点? 我想对查询表达方法中的实体使用linq.我尝试过:

Now i want retrieve all people that have role as 'barman'. How can i achieve this? I want use linq to entities in the query expression method. I've tried:

var listPerson = (from p in SiContext.People
                 where p.Role.Name = 'barman'
                 select p).ToList();

问题是我无法创建p.Ruolo.Name,因为p.Ruolo是一个ICollectionType,它没有属性"Name"(而实体Role具有该属性)

The problem is that i cannot make p.Ruolo.Name because p.Ruolo is a ICollectionType that doesn't have the property "Name" (while the entity Role has that property)

推荐答案

由于角色是集合,因此需要使用Any

Since role is a collection, you need to use Any

var listPerson = (from p in SiContext.People
                 where p.Role.Any(x => x.Name == "barman")
                 select p).ToList();

这篇关于Linq到实体的导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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