Consider this existing linq statement:
IEnumerable<string> queries = LandingSilo.Relationships
.Where(x => x.Type == 1 && x.RootKey == root.Key)
.Join(
nodes,
r => r.NodeKey,
n => n.Key,
(r, n) => n.Queries.SingleOrDefault(q => q.Seq == r.QueryId))
.Where(q => q != null)
.Select(q => q.Query);
This query doesn't quite do what I need it to do. It only exposes variables from 'n.Queries', whereas I need to access another variable from 'n' itself, further up the tree.
Required: n.Url and q.Query - output as a KeyValuePair<string,string>.
As you can see, the query links two lists - LandingSilo.Relationships and a list represented by the variable nodes.
Nodes also has a nested list property which contains one of the keys needed to join the lists together, here are the keys:
LandingSilo.Relationships: NodeKey, QueryId
nodes: Key, Queries.Seq (Queries is the list inside nodes)
Here are the specs:
LandingSilo.Relationships: List<SiloRelationship>();
public class SiloRelationship
{
public SiloRelationship(int type, string rootKey, string nodeKey, int queryId)
{
Type = type;
RootKey = rootKey;
NodeKey = nodeKey; // Key 1
QueryId = queryId; // Key 2
}
public int Type { get; set; }
public string RootKey { get; set; }
public string NodeKey { get; set; }
public int QueryId { get; set; }
}
nodes: List<SiloNode>();
public class SiloNode
{
public string Key { get; private set; } // Key 1
public string Url { get; private set; }
public List<NodeQuery> Queries { get; private set; }
}
public class NodeQuery
{
public string Query { get; private set; }
public int Seq { get; private set; } // Key 2
}
Should be able to select the values as the result of the join as a KeyValuePair
IEnumerable<string> queries = LandingSilo.Relationships
.Where(x => x.Type == 1 && x.RootKey == root.Key)
.Join(
nodes,
r => r.NodeKey,
n => n.Key,
(r, n) => new KeyValuePair<string,string>(n.Url, n.Queries.SingleOrDefault(q => q.Seq == r.QueryId)?.Query))
.Where(q => q.Value != null);
You can use intermediate anonymous projections to provide access to the other objects from the query, but it leads to quite unreadable code.
For queries involving joins the LINQ query syntax is much more appropriate due to transparent identifier access. Rewriting your query with query syntax could be like this:
var query =
from r in relationships
where r.Type == 1 && r.RootKey == root.Key
join n in nodes on r.NodeKey equals n.Key
from q in n.Queries
// Here you have access to r,n and q
where r.QueryId == q.Seq
select new KeyValuePair<string, string>(n.Url, q.Query);
Related
Given the following Models
public class ApiImageModel
{
public int ID { get; set; }
...
public List<TagModel> Tags { get; set; } = new();
}
and
public class TagModel
{
public int ID { get; set; }
...
public string Name { get; set; }
public List<ApiImageModel> Images { get; set; } = new();
}
How to query a list of ApiImageModel based on a given set of TagModels using Linq?
I am struggling with this for a while now and I'm certainly missing something basic but I can't put a pin on it.
I tried this approach for EF6:
EF6 How to query where children contains all values of a list
like so, holding all TagModel-IDs in an array "tagIDs":
int[] tagIDs;
...
IQueryable<ApiImageModel> images = context.Images.Where(image => tagIDs.All(id => image.Tags.Any(tag => tag.ID == id)));
But visual studio rewards me with an "InvalidOperationException":
The LINQ expression 'DbSet<ApiImageModel>()
.Where(a => __tagIDs_0
.All(id => DbSet<Dictionary<string, object>>("ApiImageModelTagModel")
.Where(a0 => EF.Property<Nullable<int>>(a, "ID") != null && object.Equals(
objA: (object)EF.Property<Nullable<int>>(a, "ID"),
objB: (object)EF.Property<Nullable<int>>(a0, "ImagesID")))
.Join(
inner: DbSet<TagModel>(),
outerKeySelector: a0 => EF.Property<Nullable<int>>(a0, "TagsID"),
innerKeySelector: t => EF.Property<Nullable<int>>(t, "ID"),
resultSelector: (a0, t) => new TransparentIdentifier<Dictionary<string, object>, TagModel>(
Outer = a0,
Inner = t
))
.Any(ti => ti.Inner.ID == id)))' could not be translated.
I'd be glad for some help :)
Assuming that your tags tagIDs are unique, you can do the following:
int[] tagIDs;
var tagCount = tagIDs.Length;
...
var images = context.Images
.Where(image => image.Tags.Where(tag => tagIDs.Contains(tag.ID)).Count() == tagCount);
Here we use Contains to grab tags in which we are interested and if their Count() is equal to tagIDs.Length - all tags are present in image's Tags relation.
I have a list of the following class:
public class SiloRelationship
{
public int RelationshipType { get; set; }
public string MasterKey { get; set; }
public string SlaveKey { get; set; }
public int QueryId { get; set; }
}
I have a second list of the following class:
public class SiloNode
{
public string Key { get; private set; }
public string Url { get; private set; }
public List<NodeQuery> Queries { get; private set; }
}
Which has a sub-class:
public class NodeQuery
{
public string Query { get; private set; }
public int Seq { get; private set; }
}
Lists:
LandingSilo.Relationships is a list of SiloRelationship
LandingSilo.Nodes is a list of SiloNode.
Here's my query - there is a simple join, after which I need to return the Url and Query properties - the filter should result in a single QueryNode from the list.
What we have is:
SiloRelationship => 1 to 1 SiloNode => 1 to many QueryNode
A Kvp would be adequate for the purpose of the exercise but I can't see the Query property with the code I've got so far.
var query =
from r in LandingSilo.Relationships
join n in LandingSilo.Nodes on r.SlaveKey equals n.Key
where r.RelationshipType == 1 &&
n.Queries.Select(y => y.Seq).Contains(r.QueryId)
Any help appreciated.
Try this:
IEnumerable<string> queries = LandingSilo.Relationships
.Where(r => r.RelationshipType == 1)
.Join(
LandingSilo.Nodes,
r => r.SlaveKey,
n => n.Key,
(r, n) => n.Queries.SingleOrDefault(q => q.Seq == r.QueryId))
.Where(q => q != null)
.Select(q => q.Query);
Line by line: filter all Relationships with type different from 1, join on SlaveKey/Key and select the only query in the node that has the Seq equal to the Relationships QueryId. Filter out null results and select the Query property. This is going to throw an InvalidOperationException if there are multiple queries within one node matching.
This can be also done in the LINQ keyword syntax like this:
IEnumerable<string> queries =
from r in LandingSilo.Relationships
where r.RelationshipType == 1
join n in LandingSilo.Nodes on r.SlaveKey equals n.Key
from q in n.Queries.SingleOrDefault(q => q.Seq == r.QueryId)
where q != null
select q.Query;
You just need to filter the Queries. Change last statement like below
select n.Queries.FirstOrDefault(q => q.Seq == q.QueryId);
I have 3 classes and trying to use LINQ methods to perform an INNER JOIN and a LEFT JOIN. I'm able to perform each separately, but no luck together since I can't even figure out the syntax.
Ultimately, the SQL I'd write would be:
SELECT *
FROM [Group] AS [g]
INNER JOIN [Section] AS [s] ON [s].[GroupId] = [g].[Id]
LEFT OUTER JOIN [Course] AS [c] ON [c].[SectionId] = [s].[Id]
Classes
public class Group {
public int Id { get; set; }
public int Name { get; set; }
public bool IsActive { get; set; }
public ICollection<Section> Sections { get; set; }
}
public class Section {
public int Id { get; set; }
public int Name { get; set; }
public int GroupId { get; set; }
public Group Group { get; set; }
public bool IsActive { get; set; }
public ICollection<Course> Courses { get; set; }
}
public class Course {
public int Id { get; set; }
public int UserId { get; set; }
public int Name { get; set; }
public int SectionId { get; set; }
public bool IsActive { get; set; }
}
Samples
I want the result to be of type Group. I successfully performed the LEFT JOIN between Section and Course, but then I have an object of type IQueryable<a>, which is not what I want, sinceGroup`.
var result = db.Section
.GroupJoin(db.Course,
s => s.Id,
c => c.SectionId,
(s, c) => new { s, c = c.DefaultIfEmpty() })
.SelectMany(s => s.c.Select(c => new { s = s.s, c }));
I also tried this, but returns NULL because this performs an INNER JOIN on all tables, and the user has not entered any Courses.
var result = db.Groups
.Where(g => g.IsActive)
.Include(g => g.Sections)
.Include(g => g.Sections.Select(s => s.Courses))
.Where(g => g.Sections.Any(s => s.IsActive && s.Courses.Any(c => c.UserId == _userId && c.IsActive)))
.ToList();
Question
How can I perform an INNER and a LEFT JOIN with the least number of calls to the database and get a result of type Group?
Desired Result
I would like to have 1 object of type Group, but only as long as a Group has a Section. I also want to return the Courses the user has for the specific Section or return NULL.
I think what you ask for is impossible without returning a new (anonymous) object instead of Group (as demonstrated in this answer). EF will not allow you to get a filtered Course collection inside a Section because of the way relations and entity caching works, which means you can't use navigational properties for this task.
First of all, you want to have control over which related entities are loaded, so I suggest to enable lazy loading by marking the Sections and Courses collection properties as virtual in your entities (unless you've enabled lazy loading for all entities in your application) as we don't want EF to load related Sections and Courses as it would load all courses for each user anyway.
public class Group {
public int Id { get; set; }
public int Name { get; set; }
public bool IsActive { get; set; }
public virtual ICollection<Section> Sections { get; set; }
}
public class Section {
public int Id { get; set; }
public int Name { get; set; }
public int GroupId { get; set; }
public Group Group { get; set; }
public bool IsActive { get; set; }
public virtual ICollection<Course> Courses { get; set; }
}
In method syntax, the query would probably look something like this:
var results = db.Group
.Where(g => g.IsActive)
.GroupJoin(
db.Section.Where(s => s.IsActive),
g => g.Id,
s => s.GroupId,
(g, s) => new
{
Group = g,
UserSections = s
.GroupJoin(
db.Course.Where(c => c.IsActive && c.UserId == _userId).DefaultIfEmpty(),
ss => ss.Id,
cc => cc.SectionId,
(ss, cc) => new
{
Section = ss,
UserCourses = cc
}
)
})
.ToList();
And you would consume the result as:
foreach (var result in results)
{
var group = result.Group;
foreach (var userSection in result.UserSections)
{
var section = userSection.Section;
var userCourses = userSection.UserCourses;
}
}
Now, if you don't need additional filtering of the group results on database level, you can as well go for the INNER JOIN and LEFT OUTER JOIN approach by using this LINQ query and do the grouping in-memory:
var results = db.Group
.Where(g => g.IsActive)
.Join(
db.Section.Where(s => s.IsActive),
g => g.Id,
s => s.GroupId,
(g, s) => new
{
Group = g,
UserSection = new
{
Section = s,
UserCourses = db.Course.Where(c => c.IsActive && c.UserId == _userId && c.SectionId == s.Id).DefaultIfEmpty()
}
})
.ToList() // Data gets fetched from database at this point
.GroupBy(x => x.Group) // In-memory grouping
.Select(x => new
{
Group = x.Key,
UserSections = x.Select(us => new
{
Section = us.UserSection,
UserCourses = us.UserSection.UserCourses
})
});
Remember, whenever you're trying to access group.Sections or section.Courses, you will trigger the lazy loading which will fetch all child section or courses, regardless of _userId.
Use DefaultIfEmpty to perform an outer left join
from g in db.group
join s in db.section on g.Id equals s.GroupId
join c in db.course on c.SectionId equals s.Id into courseGroup
from cg in courseGroup.DefaultIfEmpty()
select new { g, s, c };
Your SQL's type is not [Group] (Type group would be: select [Group].* from ...), anyway if you want it like that, then in its simple form it would be:
var result = db.Groups.Where( g => g.Sections.Any() );
However, if you really wanted to convert your SQL, then:
var result = from g in db.Groups
from s in g.Sections
from c in s.Courses.DefaultIfEmpty()
select new {...};
Even this would do:
var result = from g in db.Groups
select new {...};
Hint: In a well designed database with relations, you very rarely need to use join keyword. Instead use navigational properties.
How to write 'Where Any In' in LINQ to Entity?
Here is my model :
class Chair
{
public int Id { get; set; }
public int TableId { get; set; }
public Table Table { get; set; }
}
class Table
{
public int Id { get; set; }
public ICollection<Chair> Chairs { get; set; }
public ICollection<Category> Categories { get; set; }
public Table()
{
Chairs = new List<Chair>();
Categories = new List<Category>();
}
}
class Category
{
public int Id { get; set; }
public ICollection<Table> Tables { get; set; }
}
I also got a simple list of Category :
List<Category> myCategories = new List<Category>(c,d,e);
I want to get only that Chairs that belongs to Table that got one of the Category from myCategories List. Thats what im trying to do :
var result =
ctx.Chairs.Where(x => x.Table.Categories.Any(y => myCategories.Any(z => z.Id == y.Id))).ToList();
I think its ok but what i get is error :
"Unable to create a constant value of type 'ConsoleApplication1.Category'. Only primitive types or enumeration types are supported in this context"
Try to compare with in-memory categories Ids collection, instead of categories collection.
var myCategoriesIds = myCategories.Select(c => c.Id).ToArray();
var result =
context.Chairs
.Where(
x => x.Table.Categories.Any(
y => myCategoriesIds.Contains(y.Id)))
.ToList();
this is because ctx.Chairs is a collection that is in database, you should retrieve that collection first in order to compare it with in-memory data:
var result = ctx
.Chairs
.AsEnumerable() // retrieve data
.Where(x =>
x.Table.Categories.Any(y =>
myCategories.Any(z => z.Id == y.Id)))
.ToList();
EDIT: that wouldn't be the correct thing to do if you have a lot of entities on database, what you can do is to split it into two queries:
var tables = ctx.Tables
.Where(x =>
x.Categories.Any(y =>
myCategories.Any(z => z.Id == y.Id)));
var result = ctx.Chairs
.Where(x =>
tables.Any(t=> t.Id == x.TableId))
.ToList();
You can select Ids from myCategories and use it last statement.
var CategoryIds = myCategories.Select(ct => ct.Id);
var result = ctx.Chairs.Where(x => x.Table.Categories.Any(y => CategoryIds.Any(z => z == y.Id))).ToList();
I have an IEnumerable<RuleSelection> with these properties:
public class RuleSelection{
public int RuleId { get; set;}
public int? CriteriaId { get; set; }
public int? CriteriaSourceId{ get; set; }
}
RuleId in RuleSelection is not unique.
Can I write a linq query to normalize these into IEnumerable<Rule> which would be:
public class Rule{
public int RuleId { get; set; }
public IEnumerable<int> Criteria { get; set; }
public IEnumerable<int> CriteriaSource { get; set; }
}
Rule.RuleId would be unique and the properties Criteria and CriteriaSource would include all the CriteriaId's and CriteriaSourceId's for the RuleId respectively.
It sounds like you want something like:
var rules = selections.GroupBy(rs => rs.RuleId)
.Select(g => new Rule {
RuleId = g.Key,
Criteria = g.Select(rs => rs.CriteriaId)
.Where(c => c != null)
.Select(c => c.Value)
.ToList(),
CriteriaSource = g.Select(rs => rs.CriteriaSourceId)
.Where(c => c != null)
.Select(c => c.Value)
.ToList(),
});
Using my FullOuterGroupJoin extension method
LINQ - Full Outer Join
you could:
theRules.FullOuterGroupJoin(theRules,
r => r.RuleId,
r => r.RuleId,
(crit, critSource, id) => new Rule {
RuleId = id,
Criteria = crit
.Where(r => r.CriteriaId.HasValue)
.Select(r => r.CriteriaId.Value),
CriteriaSource = critSource
.Where(r => r.CriteriaSourceId.HasValue)
.Select(r => r.CriteriaSourceId.Value),
}
);
To write this:
var rules =
from sel in selections
group sel by sel.RuleId into rg
select new Rule {
RuleId = rg.Key,
Criteria = rg.Select(r => r.CriteriaId).FilterValues(),
CriteriaSource = rg.Select(r => r.CriteriaSourceId).FilterValues(),
};
I created the following FilterValues extension (to eliminate duplication):
public static IEnumerable<T> FilterValues<T>(
this IEnumerable<T?> items)
where T : struct
{
// omitting argument validation
return
from item in items
where item.HasValue
select item.Value;
}
I set out to provide essentially a pure query-syntax version of JonSkeet's answer. I gave up on that in effort to remove duplication for the property assignments and wound up with this combination extension & query-syntax approach.