I am trying to refactor a class which extract from a DB some objects of type QuerySuggest. Two queries are made on the objects returned from the DB, using Linq.
It's evident that the two queries are almost identical, the only difference is that in the former I have an additional condition : " && e.UserId == request.UserId".
This seems to me as a code smell, and I would like to refactor it, but I'm not sure on how to proceed.
this is the sample of code I need to refactor:
IRepository<QuerySuggest> repository = _repositoryManager.GetRepository<QuerySuggest>(_repositoryType);
//entitiesByUser contains all the query suggest by the user
var entitiesByUser = repository.Query(c => c.Where(e => e.IdWebsite == request.WebSiteId &&
e.FulltextFree != null &&
e.DataOra >= (System.DateTime.Today.AddDays(-60).Date) &&
e.UserId == request.UserId)
.GroupBy(g => g.FulltextFree)
.Select(n => new { FulltextFree = n.Key, HowMany = n.Count() })
.Where(w => w.HowMany >= request.HowMany)
.OrderBy(o => o.HowMany))
.ToList();
//entitiesByUser contains all the query suggest not from user
var nonUserEntities = repository.Query(c => c.Where(e => e.IdWebsite == request.WebSiteId &&
e.FulltextFree != null &&
e.DataOra >= (System.DateTime.Today.AddDays(-60).Date))
.GroupBy(g => g.FulltextFree)
.Select(n => new { FulltextFree = n.Key, HowMany = n.Count() })
.Where(w => w.HowMany >= request.HowMany)
.OrderBy(o => o.HowMany))
.ToList();