使用EntityReference查询问题

Pau*_*aul 2 asp.net linq-to-entities entity-framework entityreference

当我执行代码时:

        public List<T> GetCustomerTxList(int customerId)
        {
            var matchingPocos = new List<T>();

            using (linq.AOMSEntities dataRepos = new linq.AOMSEntities())
            {        
                IEnumerable txlist = from t in dataRepos.TransactionRecord
                                 where t.CustomerReference.Value.Id == customerId
                                 select t;

                foreach (EntityObject entity in txlist)
                {
                    matchingPocos.Add(entity.ConvertToPoco<T>());
                }
            }
            return matchingPocos;
        }
Run Code Online (Sandbox Code Playgroud)

我收到以下异常:Data.Repository.Integration.Test.LinqRepositoryTest.GetCustomerTxList:System.NotSupportedException:LINQ to Entities不支持指定的类型成员'CustomerReference'。仅支持初始化程序,实体成员和实体导航属性。

CustomerReference是在TransactionRecord实体上引用Customer实体的EntityReference。

为什么我不能使用实体引用进行查询?

建议执行这种查询的方法是什么?

如果有帮助,我将很乐意提供更多的信息/代码。

ben*_*wey 5

您应该能够像这样直接在查询中访问客户:

from t in dataRepos.TransactionRecord 
where t.Customer.Id == customerId 
select t;
Run Code Online (Sandbox Code Playgroud)

在这种情况下,EF将为您使用CustomerReference。