I'm trying to filter the posts that are displayed in my application. Everything goes like expected, but I have a small issue. A user can choose the education(s) and profession(s) he follows. How can I filter based on the arrays I get? I tried something like this, but it feels ugly. If I set more arrays in my Filter class like Language[].. It wil get more messy. Can I do something easier?
public class Filter
{
public string[] Education { get; set; }
public string[] Profession { get; set; }
public int PageIndex { get; set; }
}
Example request:
public PaginatedResults<Post> FilterPosts(Filter filter)
{
// Both Education and Profession arrays are empty, we just return all the posts
if(filter.Profession.Any(prof => prof == null) && filter.Education.Any(study => study == null)) {
var posts1 = _dbContext.Posts.AsEnumerable();
return _searchService.Pagination<Post>(posts1, filter.PageIndex);
}
else
{
// Can this be simplified? Sometimes the Education array is empty and sometimes Profession array. User can choose
IEnumerable<Post> posts = null;
if(filter.Profession.Any(prof => prof == null))
{
posts = _dbContext.Posts.Where(post => filter.Education.Contains(post.Education)).AsEnumerable();
}
else if(filter.Education.Any(study => study == null))
{
posts = _dbContext.Posts.Where(post => filter.Profession.Contains(post.Profession)).AsEnumerable();
}
else
{
posts = _dbContext.Posts.Where(post => filter.Profession.Contains(post.Profession) && filter.Education.Contains(post.Education)).AsEnumerable();
}
return _searchService.Pagination<Post>(posts, filter.PageIndex);
}
}
There probably quite a few ways you could approach this problem. Assuming you want to keep your approach (which I think is perfectly valid), you could try the following steps:
Leverage IQueryable
Assuming you use entity framework, I believe _dbContext.Posts implements IQueryable already. Since LINQ does not get executed immediately, we can build filtering conditions sequentially before enumerating the collection:
posts = _dbContext.Posts.Where(post => filter.Education.Contains(post.Education) && filter.Education.Contains(post.Profession)).AsEnumerable();
// since you are implementing `AND` semantics for your filters, is easy to break down into series of `.Where()` calls
posts = _dbContext.Posts.Where(post => filter.Education.Contains(post.Education))
.Where(post => filter.Education.Contains(post.Profession))
.AsEnumerable(); // this should filter Posts by Education AND Profession as well as represent the result as IEnumerable. Should be functionally identical to the first statement
Invert boolean conditions and check if filters have values
This will allow you to add a .Where filter only when it's needed:
if (filter.Profession.Any()) // if Profession has elements
{
posts = posts.Where(post => filter.Profession.Contains(post.Profession)); // apply respective filter to posts, you may want to ensure you only compare against meaningful search terms by appplying `.Where(i => !string.IsNullOrWhiteSpace(i))` to it
}
if (filter.Education.Any()) // if Education has elements
{
posts = posts.Where(post => filter.Education.Contains(post.Education)).AsEnumerable(); // apply respective filter to posts
}
Then, to put it all together
public PaginatedResults<Post> FilterPosts(Filter filter)
{
IQueryable<Post> posts = _dbContext.Posts;
if (filter.Profession.Any()) posts = posts.Where(post => filter.Profession.Contains(post.Profession));
if (filter.Education.Any()) posts = posts.Where(post => filter.Education.Contains(post.Education));
return _searchService.Pagination<Post>(posts.AsEnumerable(), filter.PageIndex);
}
Related
I have two models:
public class Employee
{
public int Id { get; set; }
public IList<Skill> { get; set; }
}
public class Skill
{
public int Id { get; set; }
}
And I have filter with list of skill ids, that employee should contain:
public class Filter
{
public IList<int> SkillIds { get; set; }
}
I want to write query to get all employees, that have all skills from filter.
I tried:
query.Where(e => filter.SkillIds.All(id => e.Skills.Any(skill => skill.Id == id)));
And:
query = query.Where(e => e.Skills
.Select(x => x.Id)
.Intersect(filter.SkillIds)
.Count() == filter.SkillIds.Count);
But as a result I get exception says that query could not be translated.
It is going to be a difficult, if not impossible task, to run a query like this on the sql server side.
This is because to make this work on the SQL side, you would be grouping each set of employee skills into a single row which would need to have a new column for every skill listed in the skills table.
SQL server wasn't really made to handle grouping with an unknown set of columns passed into a query. Although this kind of query is technically possible, it's probably not very easy to do through a model binding framework like ef core.
It would be easier to do this on the .net side using something like:
var employees = _context.Employees.Include(x=>x.Skill).ToList();
var filter = someFilter;
var result = employees.Where(emp => filter.All(skillID=> emp.skills.Any(skill=>skill.ID == skillID))).ToList()
This solution works:
foreach (int skillId in filter.SkillIds)
{
query = query.Where(e => e.Skills.Any(skill => skill.Id == skillId));
}
I am not sure about it's perfomance, but works pretty fast with small amount of data.
I've also encountered this issue several times now, this is the query I've come up with that I found works best and does not result in an exception.
query.Where(e => e.Skills.Where(s => filter.SkillIds.Contains(s.Id)).Count() == filter.SkillIds.Count);
Multi-select lists in MVC don't seem to bind to complex models. Instead, they return an array of selected Id numbers.
I have such a control on a page, and I'm annoyed by the amount of conditional logic I've had to deploy to get it to work. The objects in question are a Staff object who can have TeamMember membership of none or more teams.
My objects are from entity framework. I added this to the Staff object:
public int[] SelectedTeamMembers
{
get; set;
}
I can now bind to this property in my View, and users can edit the multiselect list. On posting back the edit form, I have to do this (comments added for clarity):
//user.TeamMembers not bound, so get existing memberships
IEnumerable<TeamMember> existingTeamMembers = rep.TeamMembers_Get().Where(t => t.UserId == user.UserID);
//if array is empty, remove all team memberships & avoid null checks in else
if(user.SelectedTeamMembers == null)
{
foreach(TeamMember tm in existingTeamMembers)
{
rep.TeamMembers_Remove(tm);
}
}
else
{
// if team members have been deleted, delete them
foreach (TeamMember tm in existingTeamMembers)
{
if (!user.SelectedTeamMembers.Contains(tm.TeamId))
{
rep.TeamMembers_Remove(tm);
}
}
// if there are new team memberships, add them
foreach (int i in user.SelectedTeamMembers)
{
if (!existingTeamMembers.Select(t => t.TeamId).Contains(i))
{
TeamMember tm = new TeamMember { TeamId = i, UserId = user.UserID };
rep.TeamMembers_Change(tm);
}
}
}
I can tidy this up a bit by farming out each bit to a function, of course, but it still feels like a sledgehammer to crack a nut.
Is there a neater way of achieving this?
You should evaluate the possibility of combining your for and foreach loops into a single loop as the first step of simplifying this code.
Also, you know how to use LINQ (as evidenced by you initial Where() statement) so simplify the null conditional action as well, using LINQ and some of its helper extensions:
//user.TeamMembers not bound, so get existing memberships
IEnumerable<TeamMember> existingTeamMembers = rep.TeamMembers_Get().Where(t => t.UserId == user.UserID);
//if array is empty, remove all team memberships & avoid null checks in else
if(user.SelectedTeamMembers == null)
{
existingTeamMembers.ToList().ForEach(tm => rep.TeamMembers_Remove(tm));
}
else
{
// if team members have been deleted, delete them
existingTeamMembers.Where(tm => !user.SelectedTeamMembers.Contains(tm.TeamId)).ToList().ForEach(tm => rep.TeamMembers_Remove(tm));
// if there are new team memberships, add them
user.SelectedTeamMembers.Except(existingTeamMembers.Select(t=> t.TeamId)).ToList().ForEach(i =>
{
TeamMember tm = new TeamMember { TeamId = i, UserId = user.UserID };
rep.TeamMembers_Change(tm);
});
}
While this has not decreased the conditional complexity (as in all the conditionals are still there) the syntax is a lot more readable.
You could do it this way...It relies on using the RemoveRange method.
Entity - I'm using my own for demo purposes
public class User
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public String Name { get; set; }
}
Action
public ActionResult Action(Guid[] selectedTeamMembers)
{
using (var ctx = new DatabaseContext())
{
//
// Start by targeting all users!
//
var usersToRemove = ctx.Users.AsQueryable();
//
// if we have specified a selection then select the inverse.
//
if (selectedTeamMembers != null)
{
usersToRemove = usersToRemove.Where(x => !selectedTeamMembers.Contains(x.Id));
}
//
// Use the Set Generic as this gives us access to the Remove Range method
//
ctx.Set<User>().RemoveRange(usersToRemove);
ctx.SaveChanges();
}
return View();
}
Hope this helps.
I've tried many different strategies for indexing my data but can't seem to figure it out by myself.
I'm building a database over users and their games. The users can supply the database with games they own and would like to trade as well as a list of games they would like to have:
public class Member : EntityBase
{
public List<Game> TradeList { get; set; }
public List<Game> WishList { get; set; }
}
I'm trying to create and index I can query in the form of "Give me a list of all games (with corresponding members) which have games in their TradeList matching my WishList as well as having games in their WishList matching my TradeList".. and of course, myself excluded.
I tried creating a MultiMapIndex:
public class TradingIndex : AbstractMultiMapIndexCreationTask<TradingIndex.Result>
{
public enum ListType
{
Wishlist,
Tradelist
}
public class Result
{
public string Game { get; set; }
public string Member { get; set; }
public ListType List { get; set; }
}
public TradingIndex()
{
AddMap<Member>(members => from member in members
from game in member.TradeList
select new Result()
{
Game = game.Id,
Member = member.Id,
List = ListType.Tradelist
});
AddMap<Member>(members => from member in members
from game in member.WishList
select new Result()
{
Game = game.Id,
Member = member.Id,
List = ListType.Wishlist
});
}
}
And then querying it like this:
db.Query<TradingIndex.Result, TradingIndex>()
.Where(g =>
(g.Game.In(gamesIWant) && g.List == TradingIndex.ListType.Tradelist)
&&
(g.Game.In(gamesITrade) && g.List == TradingIndex.ListType.Wishlist)
&&
g.Member != me.Id
)
But I can't get that to work. I've also looked at Map/Reduce, but the problem I have seem to be getting RavenDB to give me the correct result type.
I hope you get what I'm trying to do and can give me some hints on what to look into.
First, you'll need to make sure that you store the fields you are indexing. This is required so you can get the index results back, instead of the documents that matched the index.
Add this to the bottom of your index definition:
StoreAllFields(FieldStorage.Yes);
Or if you want to be more verbose, (perhaps your index is doing other things also):
Store(x => x.Game, FieldStorage.Yes);
Store(x => x.Member, FieldStorage.Yes);
Store(x => x.List, FieldStorage.Yes);
When you query this, you'll need to tell Raven to send you back the index entries, by using ProjectFromIndexFieldsInto as described here.
Next, you need to realize that you aren't creating any single index entry that will match your query. The multimap index is creating separate entries in the index for each map. If you want to combine them in your results, you'll need to use an intersection query.
Putting this together, your query should look like this:
var q = session.Query<TradingIndex.Result, TradingIndex>()
.Where(g => g.Game.In(gamesIWant) &&
g.List == TradingIndex.ListType.Tradelist &&
g.Member != me.Id)
.Intersect()
.Where(g => g.Game.In(gamesITrade) &&
g.List == TradingIndex.ListType.Wishlist &&
g.Member != me.Id)
.ProjectFromIndexFieldsInto<TradingIndex.Result>();
Full test in this GIST.
Here's my problem: I have a class that have 2 list properties of the same class type (but with some different restriction as on how to be filled), let's say:
public class Team
{
[Key]
public int IDTeam { get; set; }
public string TeamName { get; set; }
public List<Programmer> Members { get; set; }
public List<Programmer> Leaders { get; set; }
public LoadLists(MyProjectDBContext db)
{
this.Members = db.Programmers.Where(p => p.IDTeam = this.IDTeam
&& (p.Experience == "" || p.Experience == null)).ToList();
this.Leaders = db.Programmers.Where(p => p.IDTeam = this.IDTeam
&& (p.Experience != null && p.Experience != "")).ToList();
}
}
public class Programmer
{
[Key]
public int IDProgrammer { get; set; }
[ForeignKey("Team")]
public int IDTeam { get; set; }
public virtual Team Team { get; set; }
public string Name { get; set; }
public string Experience { get; set; }
}
At some point, I need to take a list of Teams, with it's members and leaders, and for this I would assume something like:
return db.Teams
.Include(m => m.Members.Where(p => p.Experience == "" || p.Experience == null)
.Include(l => l.Leaders.Where(p => p.Experience != null && p.Experience != "")
.OrderBy(t => t.TeamName)
.ToList();
And, of course, in this case I would be assuming it wrong (cause it's not working at all).
Any ideas on how to achieve that?
EDIT: To clarify a bit more, the 2 list properties of the team class should be filled according to:
1 - Members attribute - Should include all related proggramers with no experience (proggramer.Experience == null or "");
2 - Leaders attribute - Should include all related proggramers with any experience (programmer.Experiente != null nor "");
EDIT 2: Here's the MyProjectDbContext declaration:
public class MyProjectDBContext : DbContext
{
public DbSet<Team> Teams { get; set; }
public DbSet<Programmer> Programmers { get; set; }
}
You are talking about EntityFramework (Linq to entities) right? If so, Include() is a Method of Linq To Entities to include a sub-relation in the result set. I think you should place the Where() outside of the Inlcude().
On this topic you'll find some examples on how to use the Include() method.
So I suggest to add the Include()'s first to include the relations "Members" and "Leaders" and then apply your Where-Statement (can be done with one Where()).
return db.Teams
.Include("Team.Members")
.Include("Team.Leaders")
.Where(t => string.IsNullOrWhitespace(t.Members.Experience) ... )
What is unclear to me is your where criteria and your use-case at all as you are talking of getting a list of Teams with Leaders and Members. May above example will return a list of Teams that match the Where() statement. You can look though it and within that loop you can list its members and leaders - if that is the use-case.
An alternative is something like this:
return db.Members
.Where(m => string.IsNullOrWhitespace(m.Experience))
.GroupBy(m => m.Team)
This get you a list of members with no experience grouped by Team. You can loop the groups (Teams) and within on its members. If you like to get each team only once you can add a Distinct(m => m.Team) at the end.
Hope this helps. If you need some more detailed code samples it would help to understand your requirements better. So maybe you can say a few more words on what you expect from the query.
Update:
Just read our edits which sound interesting. I don't think you can do this all in one Linq-To-Entities statement. Personally I would do that on the getters of the properties Members and Leaders which do their own query (as a read-only property). To get performance for huge data amount I would even do it with SQL-views on the DB itself. But this depends a little on the context the "Members" and "Leaders" are used (high frequent etc).
Update 2:
Using a single query to get a table of teams with sublists for members and leaders I would do a query on "Programmers" and group them nested by Team and Experience. The result is then a list of groups (=Teams) with Groups (Experienced/Non-experience) with Programmers in it. The final table then can be build with three nested foreach-Statements. See here for some grouping examples (see the example "GroupBy - Nested").
Whenever you fetch entities, they will be stored in the context -- regardless of the form they are "selected" in. That means you can fetch the teams along with all the necessary related entities into an anonymous type, like this:
var teams =
(from team in db.Teams
select new {
team,
relatedProgrammers = team.Programmers.Where(
[query that gets all leaders OR members])
}).ToList().Select(x => x.team);
It looks like we're throwing away the relatedProgrammers field here, but those Programmer entities are still in memory. So, when you execute this:
foreach (var team in teams) team.LoadLists(db);
...it will populate the lists from the programmers that were already fetched, without querying the database again (assuming db is the same context instance as above).
Note: I haven't tested this myself. It's based on a similar technique shown in this answer.
EDIT - Actually, it looks like your "leaders" and "members" cover all programmers associated with a team, so you should be able to just do Teams.Include(t => t.Programmers) and then LoadLists.
Utilizing NHibernate I am attempting to use a lambda expression to retrieve objects based on the status and values between a parent child relationship. AbstractWorkflowRequestInformation has a collection of WorkflowRequestInformationAction. Each of the two classes have their own Status properties. To illustrate here are the abbreviated classes as they relate to this query:
public class AbstractWorkflowRequestInformation
{
public virtual RequestStatus RequestStatus { get; set; }
public virtual IEnumerable<WorkflowRequestInformationAction>
WorkflowRequestInformationActionList { get; set; }
}
public class WorkflowRequestInformationAction
{
public virtual ActionStatus Status { get; set; }
public virtual string RoleIdentifier { get; set; }
public virtual string RoleName { get; set; }
}
Given this relationship I want to retrieve AbstractWorkflowRequestInformation objects based on a List<KeyValuePair<string, string>> called roles. I realize that the exception is being caused by a lack of parsing of the Any(...) extension method, but I am unsure of alternative queries. Thus far all permutations on the below query have caused the same or similar exceptions:
public IEnumerable<IRequestInformation> GetRequestsPendingActionBy(
List<KeyValuePair<string, string>> roles)
{
var results = GetSession().Query<AbstractWorkflowRequestInformation>()
.Where(r => r.RequestStatus == RequestStatus.Pending
&& r.WorkflowRequestInformationActionList
.Any(a => ActionStatus.Pending == a.Status
&& roles.Any(kp => kp.Key == a.RoleName
&& kp.Value == a.RoleIdentifier)))
.ToList();
return results;
}
The ultimate goal is to retrieve only those AbstractWorkflowRequestInformation objects which are pending and have a pending WorkflowRequestInformationAction matching a KeyValuePair in the roles enumerable.
I am not wedded to using a lambda expression as this expression has already grown unwieldy, if there's a more elegant ICriteria expression I am all ears. What are my options to restrict my results based upon the values in my roles List<KeyValuePair<string, string>> but prevent the "Specified method is not supported" exception?
I think this would get same results...
WorkflowRequestInformationAction actionAlias = null;
var q = GetSession().QueryOver<AbstractWorkflowRequestInformation>()
.Inner.JoinAlias(x => x.WorkflowRequestInformationActionList,
() => actionAlias)
.Where(x => x.RequestStatus == RequestStatus.Pending)
.And(() => actionAlias.Status == ActionStatus.Pending);
var d = Restrictions.Disjunction();
foreach(var kvp in roles)
{
d.Add(Restrictions.Where(() => actionAlias.RoleName == kvp.Key
&& actionAlias.RoleIdentitifier == kvp.Value));
}
q.And(d).TransformUsing(Transformers.DistinctRootEntity);
var results = q.List();
You could probably take a similar approach with NH Linq. I'm more comfortable with QueryOver/Criteria though.
The LINQ provider in NHibernate is not fully supported, you are trying to execute an extension method on a part of the expression tree that is not parsed from the provider.
This post might help you solve the problem. Be sure to checkout the related posts.
Also see the post from Fabio Maulo on NHibernate LINQ provider extension.