score:3
Accepted answer
Sure, this will do it:
var query = from i in list
group i by i.GroupId into g
where g.Any(p => p.ItemId == 30) == false
select g.Key;
foreach(var result in query) { Console.WriteLine(result); }
This outputs:
2
3
Here I have used, as an example:
class Product {
public int GroupId { get; set; }
public int ItemId { get; set; }
}
and
var list = new List<Product>() {
new Product() {GroupId = 1, ItemId = 10},
new Product() {GroupId = 1, ItemId = 20},
new Product() {GroupId = 1, ItemId = 30},
new Product() {GroupId = 2, ItemId = 10},
new Product() {GroupId = 2, ItemId = 20},
new Product() {GroupId = 3, ItemId = 10},
new Product() {GroupId = 3, ItemId = 20},
};
score:0
I don't have Linq, but here is the SQL Server SQL to do what you want:
DECLARE @YourTable table (ID int, value int)
insert into @YourTable VALUES (1, 10)
insert into @YourTable VALUES (1, 20)
insert into @YourTable VALUES (1, 30)
insert into @YourTable VALUES (2, 10)
insert into @YourTable VALUES (2, 20)
insert into @YourTable VALUES (3, 10)
insert into @YourTable VALUES (3, 20)
SELECT DISTINCT ID
FROM @YourTable y1
WHERE NOT EXISTS (SELECT Value
FROM @YourTable y2
WHERE y1.ID=y2.id and y2.value=30)
OUTPUT:
ID
-----------
2
3
(2 row(s) affected)
Source: stackoverflow.com
Related Articles
- Linq group by + where for each group
- How to get first record in each group using Linq
- Linq order by, group by and order by each group?
- Linq - Top value from each group
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- How to combine Where clause and group by in LINQ
- Linq GroupBy with each null value as a group
- Why C# LINQ expressions must end with Select or Group By Clause where as no such restriction in VB.Net
- LINQ WHERE method alters source collection
- Where can I view LINQ source code?
- Group by date range , count and sort within each group LINQ
- LINQ Source Code Available
- LINQ - Writing an extension method to get the row with maximum value for each group
- Linq with where clause in many-to-many EF Code First object
- Group items and select specific item from each group with LINQ
- LINQ In Clause With Group By And TOP N Records Within Each Group
- Linq query to group items and query from the top item in each group
- c# Linq - Select Top 2 from Each Group
- C# - Linq optimize code with List and Where clause
- Linq - Group then compare elements within each group
- creating Linq to sqlite dbml from DbLinq source code
- LINQ Group by and having where clause
- How to use LINQ group by with where clause
- C# Linq - group where multiple columns match?
- Linq to Entities group query giving list of results in each group
- LINQ query where complete group meets a condition
- LINQ to SQL - order by, group by and order by each group with skip and take
- Linq - Group By Id, Order By and Then select top 5 of each grouping
- LINQ method syntax to group by a column, pick one element from each group with total count per group
- Linq - group each class
- Entity Framework 6, best strategy to get filtered data from big table with many links to other tables
- Sum in Linq to Object query vb.net
- C# - Get All Words Between Chars
- does converting IQueryable to IEnumerable execute the query again?
- Select all columns after JOIN in LINQ
- How do I read Child Nodes in Xml file using MVC
- LINQ/MVC webservice return json file or an empty array
- Use LINQ to get only the most recent JOINed item for each element
- C# lambda-> All rows Select Add Row_Number
- Why am I not getting .CopyToDataTable() in Linq Query()
- Canonical Function in Linq to Entities not working
- Complex LINQ Query with Groupings (need some help)
- Linq to Find and eliminate duplicate file names
- Search in IQueryable<T>
- C# / LINQ - having trouble querying database with LINQ
- XML LINQ: How to select more than one item?
- Input string was not in a correct format error, but still able to get parsed value from LINQ to XML query
- LINQ query take(count) from collection of child elements
- lambda inner join and select all
- C# EF Linq bitwise question