views:

183

answers:

1

When I execute the code:

        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;
        }

I get the following exception: Data.Repository.Integration.Test.LinqRepositoryTest.GetCustomerTxList: System.NotSupportedException : The specified type member 'CustomerReference' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

CustomerReference is an EntityReference on the TransactionRecord entity referencing a Customer entity.

Why can I not query using an entity reference?

What is the recommended approach to perform such a query?

I'll gladly provide more info/code if it helps.

+3  A: 

You should be able to access the Customer directly in your query like this:

from t in dataRepos.TransactionRecord 
where t.Customer.Id == customerId 
select t;

Under the covers EF will use the CustomerReference for you in this case.

bendewey
Thanks, works a treat. Is it a restriction in EF that you can't perform queries using an EntityReference?
Paul
@Paul, not a restriction as far as I'm concerned, this way is much more readable.
bendewey
@bendewey, I agree it's more readable. I'm looking for confirmation of my understanding, finding it hard to locate info that explains this level of detail for EF.
Paul