none
Strange: ToList() will only execute if passing a string explicitly but not passing a string variable?? WhY? RRS feed

  • Question

  • Hi all

    Here is my code which should be self explanatory:

    
            public GenericRepository(MyContext context)
            {
                this.context = context;
                this.dbSet = context.Set<TEntity>();
            }
    
            public virtual IEnumerable<TEntity> Get(
                Expression<Func<TEntity, bool>> filter = null,
                Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
                string includeProperties = "")
            {
                IQueryable<TEntity> query = dbSet;
    
                if (filter != null)
                {
                    query = query.Where(filter);
                }
     
                ...
    
                if (orderBy != null)
                {
                    return orderBy(query).ToList();
                }
                else
                {
                    return query.ToList(); //this is only executed if I explicitly pass the string but wont work passing the temp variable. why???
                }
            }
    
    
        public ActionResult _SelectAjaxEntityHistoryPartial(Guid entityID)
        {
            string temp = entityID.ToString();
    
            //IEnumerable<AuditLog> model = unitOfWork.AuditLogRepository.Get(filter: f => f.RecordID == temp); //This does NOT work!
            IEnumerable<AuditLog> model = unitOfWork.AuditLogRepository.Get(filter: f => f.RecordID == "75214687-E4CC-4690-961C-AB7383340245");
    
            return PartialView("_AuditLogPartial", model);
        }
    

    Any ideas? Thank you!


    Max
    Monday, January 16, 2012 9:10 PM

Answers

  • Hi,

    Have you tried to break into your code to see if entityId is correct ? My guess would be rather that entityId is not the value you expect.

    Or try to define temp to the same hardcoded value and see what happens with f.RecordID==temp...

     

     

     

     


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    Tuesday, January 17, 2012 6:15 PM
  • Hi Max,

    I think the problem caused by your logic in Get method. Try to remove else{} block in your code, let the query excute the code always.

    >>What it does not work is if I type "Get(filter: f => f.RecordID == entityID.ToString());"

    The provider couldn't recognise the ToString method, so it will throw a exception.

      if (orderBy != null)
                 {
                     orderBy(query);
                 }      
          return query.ToList(); <==== 
      
     
    
    Have a nice day.

     

     


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, January 18, 2012 4:00 AM
    Moderator

All replies

  • Hi Max,

    Glad to see you again!

    Based on your post, I think the two queries are same if the RecordID type is right. So please try to :

    IEnumerable<AuditLog> model = unitOfWork.AuditLogRepository.Get(filter: f => f.RecordID == entityID);
    

    Have a nice day. 


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 17, 2012 8:02 AM
    Moderator
  • hey Alan,

    RecordID is of type STRING so I need to convert GUID to STRING.

    If I put a break point on "query = query.Where(filter);" and check the filter content here is what I see:

    -If I pass the string "75214687-E4CC-4690-961C-AB7383340245":

    Body = {(f.RecordID == "75214687-E4CC-4690-961C-AB7383340245")}
    DebugView = ".Lambda #Lambda1<System.Func`2[Uds.DataModel.AuditLog,System.Boolean]>(Uds.DataModel.AuditLog $f) {\r\n    $f.RecordID == \"75214687-E4CC-4690-961C-AB7383340245\"\r\n}"

    -If I pass the variable "temp" which is entityID converted to STRING:

    Body = {(f.RecordID == value(Uds.Web.Controllers.SccmSiteController+<>c__DisplayClassc).temp)}
    DebugView = ".Lambda #Lambda1<System.Func`2[Uds.DataModel.AuditLog,System.Boolean]>(Uds.DataModel.AuditLog $f) {\r\n    $f.RecordID == .Constant<Uds.Web.Controllers.SccmSiteController+<>c__DisplayClassc>(Uds.Web.Controllers.SccmSiteController+<>c__Displ...

    Thanks


    Max
    Tuesday, January 17, 2012 5:53 PM
  • Hi,

    Have you tried to break into your code to see if entityId is correct ? My guess would be rather that entityId is not the value you expect.

    Or try to define temp to the same hardcoded value and see what happens with f.RecordID==temp...

     

     

     

     


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    Tuesday, January 17, 2012 6:15 PM
  • Trying to define temp to the same hardcoded value and running f.RecordID==temp... gives me the same problem.
    Weird. How to adjust the proper sintaxe then? It should work with a variable. I dont get it :(

    Thanks


    Max
    Tuesday, January 17, 2012 8:04 PM
  • Sorry about the confusion! What it does not work is if I type "Get(filter: f => f.RecordID == entityID.ToString());"

    The method below DOES work BUT I don't see anything coming in the SQL Profiler:
      string temp = entityID.ToString();
      IEnumerable<AuditLog> model = unitOfWork.AuditLogRepository.Get(filter: f => f.RecordID == temp);

    This ALSO works:
      IEnumerable<AuditLog> model = unitOfWork.AuditLogRepository.Get(filter: f => f.RecordID == "75214687-E4CC-4690-961C-AB7383340245");
    AND I see the call in SQL Profiler right AFTER the following line is run "return query.ToList()" :

            public virtual IEnumerable<TEntity> Get(
                Expression<Func<TEntity, bool>> filter = null,
                Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
                string includeProperties = "")
            {
                IQueryable<TEntity> query = dbSet;

                if (filter != null)
                {
                    query = query.Where(filter);
                }

    ...

                if (orderBy != null)
                {
                    return orderBy(query).ToList();
                }
                else
                {
                    return query.ToList(); <====
                }
            }

    Why?

    Thanks


    Max
    Tuesday, January 17, 2012 9:50 PM
  • Hi Max,

    I think the problem caused by your logic in Get method. Try to remove else{} block in your code, let the query excute the code always.

    >>What it does not work is if I type "Get(filter: f => f.RecordID == entityID.ToString());"

    The provider couldn't recognise the ToString method, so it will throw a exception.

      if (orderBy != null)
                 {
                     orderBy(query);
                 }      
          return query.ToList(); <==== 
      
     
    
    Have a nice day.

     

     


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, January 18, 2012 4:00 AM
    Moderator