我有一个具有以下结构的组织表
[dbo].[Organizations](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NOT NULL,
[Phone] [nvarchar](13) NULL,
[Fax] [nchar](11) NULL,
[Address] [nvarchar](100) NULL,
[URL] [varchar](50) NULL,
[Email] [nvarchar](50) NULL,
[EstablishedYear] [nchar](4) NULL,
[CategoryId] [int] NULL,
[RegionId] [int] NULL,
[CityId] [int] NULL,
[ProvinceId] [int] NULL,
[CountryId] [int] NULL,
[ImageFileName] [nvarchar](50) NULL)
因为我正在使用实体框架3.5, 我使用了一个部分类来添加外键属性(对于countryid,provinceid,...)
public partial class Organization
{
public int? CountryId
{
get
{
if (CountryReference.EntityKey == null)
return null;
return (int)CountryReference.EntityKey.EntityKeyValues[0].Value;
}
set
{
if (value != null && value != -1)
CountryReference.EntityKey = new EntityKey("Entities.Countries", "CountryId", value);
else
CountryReference.EntityKey = null;
}
}
}
现在我有一个查询,但它会引发异常:
查询:
if (Enumerable.Any(ctx.Organizations.Where(s => s.CountryId== Organization.CountryId && s.ProvinceId == Organization.ProvinceId && s.CityId == Organization.CityId && s.Name == Organization.Name)))
例外:
The specified type member 'CountryId' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
我只想比较导航属性,任何想法?
答案 0 :(得分:2)
您不能在linq-to-entities查询中使用在partial类中定义的属性。您必须直接使用导航属性:
ctx.Organizations.Where(o => o.Country.Id == someCountryId);
答案 1 :(得分:-1)
我找到了解决方案:
if (Enumerable.Any(ctx.Organizations.AsEnumerable().
Where(s => s.CountryId == Organization.CountryId &&
s.ProvinceId == Organization.ProvinceId &&
s.CityId == Organization.CityId &&
s.Name == Organization.Name)))
befor使用where条件我使用AsEnumerable()方法。