none
IQueryable generic type can not use lamda expression convert RRS feed

  • Question

  • My design code:

    publick IEntity<TID>

    {

           TID ID { get; set;}

    }

    public class CommonService<TEntity, TID>
            where TEntity : IEntity<TID>
        {
            private IRepository<TEntity> CommonRepository { get; set; }

            public IEnumerable<TEntity> FindAll()
            {
                return CommonRepository.FindAll();
            }

            public TEntity FindByID(TID id)
            {
                return CommonRepository.FindAll().FirstOrDefault(c => c.ID.Equals(ConvertToTID(id.ToString())));
            }

            public void Update(TEntity entity)
            {
                if (entity == null)
                {
                    throw new ArgumentNullException("entity");
                }

                CommonRepository.Update(entity);
            }

            public void Delete(TEntity entity)
            {
                if (entity == null)
                {
                    throw new ArgumentNullException("entity");
                }

                CommonRepository.Delete(entity);
            }

            public void Add(TEntity entity)
            {
                if (entity == null)
                {
                    throw new ArgumentNullException("entity");
                }

                CommonRepository.Add(entity);
            }

            #region Helper Method
            protected static TID ConvertToTID(string tidStr)
            {
                try
                {
                    var tidType = typeof(TID);
                    var converter = System.ComponentModel.TypeDescriptor.GetConverter(tidType);
                    return (TID)converter.ConvertFromString(tidStr);
                }
                catch
                {
                }
                return default(TID);
            }
            #endregion
        }

    we look at the "FindByID" method.

    The right code:

    return CommonRepository.FindAll().FirstOrDefault(c => c.ID.Equals(ConvertToTID(id.ToString())));

    The error code:

    return CommonRepository.FindAll().AsQueryable().FirstOrDefault(c => c.ID.Equals(ConvertToTID(id.ToString())));

    The exception as below is "The linq to entities not support the method.".

    How to write the lamada expression and can make the method right ?

    Sunday, March 16, 2014 3:51 PM

Answers

  • Hello,

    >>How to write the lamada expression and can make the method right ?

    It is not impossible to use AsQueryable(). The AsQueryable() will try to translate the query to TSQL, but method as .ToString(), .ConvertFromString() is not supported in LINQ to entities.

    We just need to perform the projection after we have the results as using FirstOrDefault() which will load data to local first.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, March 17, 2014 5:53 AM
    Moderator