简体   繁体   English

当使用group by时,LINQ从一个表中获取另一个表中不存在的行?

[英]LINQ get rows from a table that don't exist in another table when using group by?

I have two three tables books, authors and groups. 我有两本三桌书籍,分别是作者和团体。

books: bookId, name, authorId.... 书籍:bookId,名称,authorId...。

authors: authorId, firstName, LastName.... 作者:authorId,firstName,LastName ....

groups: groupID, name, authorId... 组:groupID,名称,authorId ...

i need to select all authorIds that doesn't belong to any groups and doesn't have less than 3 books published between two dates. 我需要选择不属于任何组并且在两个日期之间出版的书籍不少于3本书的所有authorIds

 List<int> authorId= new List<int>(db.Books.GroupBy(x => x.authorId)
                .Where(x => x.Any(y => y.datePublished <= now && y.datePublished >= date) && x.Count() > 2)
                .Select(x => x.FirstOrDefault().authorId)).Distinct().ToList();

Well, assuming that you're using Entity Framework and you have the proper navigation properties in your model, you can do something like this: 好吧,假设您使用的是Entity Framework,并且模型中具有适当的导航属性 ,则可以执行以下操作:

var authorIds= db.Authors
                 .Where(a=>a.Books.Count(y => y.datePublished <= now && y.datePublished >= date)>2 && !a.Groups.Any())
                 .ToList();

Another solution that came to my mind now but I'm not sure if it works is using two group joins : 现在我想到了另一个解决方案,但是我不确定是否可以使用两个组联接

var authorId=(from a in db.Authors
              join b in db.Books on a.authorId equals b.authorId into g1
              join g in db.Groups on a.authorId equals g.authorId into g2
              where  g1.Count(y => y.datePublished <= now && y.datePublished >= date)>0 && !g2.Any()
              select a.authorId).ToList();

But I definitely prefer my first solution, it's more readable and easy too. 但是我绝对更喜欢我的第一个解决方案,它也更易读和易读。

I guess this would work: 我想这会工作:

List<int> authorId = (List<int>)db.Books
    .Where(x => x.datePublished >= InitialDate)
    .Where(x => x.datePublished <= FinalDate)
    .GroupBy(x => x.authorId)
    .Select(g => new { authorId = g.Key, count = g.Count() })
    .Where(g => g.Count >= 3)
    .Select(g.authorId);

I make every single step so you can see it better, fell free to optimized as needed. 我迈出了每一步,以便您可以更好地看到它,并根据需要随意进行优化。 The key thing here is the new { authorId = g.Key, count = g.Count() } . 这里的关键是new { authorId = g.Key, count = g.Count() }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Linq to Sql:从DB1-table1中仅选择DB2-table2上不存在的项目 - Linq to Sql: Select only items from DB1-table1 that don't exist on DB2-table2 使用linq从链接到另一个表的表中获取一条记录 - using linq to get a single record from a table linked to another table LINQ Group结果并从另一个表中选择 - LINQ Group results and select from another table 如果数据存在于另一个表中,如何从表中获取数据C#LINQ - How to get data from a Table if they exist in another Table C# LINQ LINQ从一个表中获取数据,但如果不是在另一个表中? - Linq to get data from a table but not if in another table? SQL Server查询:获取另一个表的字段中不存在的列的列表 - SQL Server Query: Get a List of Columns Which Don't Exist in Another Table's Field 即使使用 Linq C# 在第二个表中没有相关信息,我也需要从表 1 中获取所有行 - i need to get all rows from table 1 even when there is no info related in second table using Linq C# 在Linq中按一个表分组并从另一个表中求和 - Group by one table and sum from another table in Linq 使用Linq按照另一个表中的记录数排序获取列表 - Get List ordered by number of records from another table using Linq C#linq:将行从一个表复制到另一个表 - C# linq : copy rows from a table to another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM