使用linq将外键值加载到实体

时间:2009-08-31 13:42:18

标签: c# asp.net linq

我有一个表Admins,它在用户和位置之间创建关系:

ID - numeric (PK)
UserId - uniqueidentifier (FK to UserId in aspnet_Users table)
LocationId - numeric

现在我要执行命令:

IQueryable<decimal> location_ids = (from m in _db.Admins
                                    where m.UserId.Equals( new Guid("c5d3dc0e-81e6-4d6b-a9c3-faa802e10b7d")) && m.LocationId.Equals(conf.umisteni.Budova.ID)
                                    select m.LocationId);

但由于某种原因(我猜UserId是FK)m.UserId无法解析。所以我试着用

m.aspnet_UsersReference.Value.UserId

然后是决定声明

if (!location_ids.Any())

失败,异常

System.NotSupportedException: The specified type member 'aspnet_UsersReference' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

我应该怎么做以获取可枚举的LocationIds列表?

提前致谢。

1 个答案:

答案 0 :(得分:2)

你错过了'用户'和点:

var location_ids = (from m in _db.Admins
                    where m.Users.UserId.Equals( 
                        new Guid("c5d3dc0e-81e6-4d6b-a9c3-faa802e10b7d")) &&   
                        m.LocationId.Equals(conf.umisteni.Budova.ID)
                    select m.LocationId);

基本上,您需要直接使用“用户”关系。

相关问题