1
var offices=  from x in _officeRepository.GetAll()
                      select new OfficeModels()
                                 {
                                     Id = x.id,
                                     AddressOne = x.address_1,
                                     AddressTwo = x.address_2,
                                     Company = x.a1.Select(y => new CompanyModels { Id=y.id, Name = y.name}).FirstOrDefault()??
                                                x.a2.Select(y => new CompanyModels { Id = y.id, Name = y.name }).FirstOrDefault() ??
                                                x.a3.Select(y => new CompanyModels { Id = y.id, Name = y.name }).FirstOrDefault() ??
                                                x.a4.Select(y => new CompanyModels { Id = y.id, Name = y.name }).FirstOrDefault(),
                                     City = x.city,
                                     Active = x.active,
                                     Country = x.country,
                                     Deleted = x.deleted,
                                     OfficeFax = x.fax,
                                     OfficeName = x.office_name,
                                     OfficePhone = x.office_phone
                                 };

我收到此错误The argument to DbIsNullExpression must refer to a primitive or reference type,该错误是由于 CompanyModels.. 我在这里做错了什么,我错过了什么吗?

如果假设我在初始化 CompanyModels 时包含此行

new CompanyModels { CompanyId = y.id, Name = y.name,CompType = Enumerations.Companies.MediaSellers }

CompType = Enumerations.Companies.MediaSellers这给出了某种不同的错误说法The specified value is not an instance of type 'Edm.Int32'Parameter name: value

4

1 回答 1

0

我不确定这个异常试图说明什么。我认为 EF 只能应用 ?? 操作符映射到一个对象,因为一切都必须翻译成 SQL。它没有 CompanyModels 的信息,所以它不知道如何处理它。

如果是这样,我认为您可以通过首先设置一个属性来解决它,比如 A,如下所示:

...
AddressTwo = x.address_2,
A = x.a1.FirstOrDefault() ?? x.a2.FirstOrDefault() ?? 
    x.a3.FirstOrDefault() ?? x.a4.FirstOrDefault(),
...

并给OfficeModels类一个像这样的公司属性:

public CompanyModels Company
{
    get
    {
        return A != null ? new CompanyModels {Id = A.id, Name = A.name} : null;
    }
}

(我有一个类似的查询工作)。

至于第二个问题:EF 不能处理枚举,所以你必须在那里处理整数。在 Stack Overflow 上查看 EF + 枚举解决方法。

于 2012-05-12T18:43:19.067 回答