I have a question for you all!!!,
Assume that I have 2 tables, one of them is a parent, and the other is a child. I want to filter the parent table by sending data from the child object. The child table contains 4 columns like
{Id, RuleId, RuleKey, RuleValue}
I send a dictionary as a request that includes value for 2 columns ( RuleKey, RuleValue) of the child object. I want to return the parent object that includes RuleKey and RuleValue I sent.
// Key is the column name of the child table and value is the corresponding value
var dictionary = new Dictionary<string,string>()
{
{"RuleKey", "RuleValue"},
{"RuleKey1", "RuleValue1"}
}
This is what I tried but it obviously won’t work and still give the syntax error.
These 2 questions about this issue;
1-) How should I do this (Important one:)))))
2-) Will be there any error If I do this In-Memory, If I remember that correctly, EFCore won’t translate the LINQ to SQL script because of the memory issues.
var rules = _ruleContext.Rules.AsQueryable();
rules = rules.Where(u => u.Criteria.Where(k => dictionary.Keys.Contains(k.Key) && dictionary.Values.Contains(k.Value)));
public class Rule : Entity<int>
{
public string RuleName { get; set; }
public string Expression { get; set; }
public ICollection<Criteria> Criteria { get; set; }
}
public class Criteria : Entity<int>
{
public int RuleId { get; set; }
public string Name { get; set; }
public string Key { get; set; }
public string Operator { get; set; }
public string Value { get; set; }
public Rule Rule { get; set; }
}
2
Answers
I’m assuming that you want to only match criteria where both
Key
andValue
match a key/value pair in the dictionary. (Your pseudocode would include any rules where either the key or value matched)What you’re trying to do is join between the
Criteria
table and the key/values in the dictionary. One way to accomplish this would be to create a temporary table containing those pairs, and then use a raw SQL statement to join fromCriteria
onto that.Something like this might work:
Edit (now without distinct):
I noticed in your comment that a rule must meet all the given criteria – this version of the query will make sure that any matching rule will have the same number of distinct criteria that were requested. That way if the same criteria key and value is duplicated in the request it won’t matter:
Edit 2:
If the
Criteria
table is going to be quite large, I’d definitely recommend you have an index on theKey
andValue
columns, otherwise you’ll be doing a clustered index scan across the entire table for every query.You can use function FilterByItems (don’t want to repeat myself). Then query can be written in the following way: