score:3
var count = 50;
var alreayUsed = from au in repository.GetEntity<AlreadyUsed>()
select au.Id;
var notUsed = (from nu in repository.GetEntity<Sample>()
where !alreadyUsed.Contains(nu.Id) && nu.References.Count() > 0
orderby nu.Name
select new CustomClass
{
CName = nu.Name,
CId = nu.Id
}).Take(count).ToArray();
Notice that I added "nu.References.Count() > 0"
I assume that you setup the association relationship called References correctly in your data model so that there are many "Reference" objects in for every Sample object.
score:0
You should be able to use the same technique from your alreadyUsed sample. EG:
var reference = from r in repository.GetEntity<Reference>()
select r.Id;
var notUsed = (from nu in repository.GetEntity<Sample>()
where !alreadyUsed.Contains(nu.Id)
&& reference.Contains(nu.Id)
select new CustomClass
{
CName = nu.Name,
CId = nu.Id
}).Take(count).ToArray();
However, if you do have an association made between the Sample table and the Reference table, then you should probably use Paul's method.
score:0
This should achieve what you are looking for. Of course there are many ways to do it. Personally I'd write it this way.
var items = (from r in repository.GetEntity<Reference>()
join s in repository.GetEntity<Sample>()
on r.FkId equals s.Id
where !repository.GetEntity<AlreadyUsed>().Contains(s.Id)
orderby s.Name
select new CustomClass
{
CName = s.Name,
CId = s.Id
})
.Take(count)
.ToArray();
Source: stackoverflow.com
Related Articles
- C# Linq query help removing foreach loops creating cleaner code
- Help with Linq query
- Help with Linq and Generics. Using GetValue inside a Query
- Need help with LINQ query and ASP.NET MVC?
- Linq sub query when using a repository pattern with EF code first
- help with linq query
- Help me with Linq query please
- Help with linq to sql query
- Help with linq query with subqueries
- Need help with a LINQ Query using the Dynamic LINQ Library
- Avoiding repeating code with Linq query + optional params
- Help with LINQ query
- Help with this Linq query (many-to-many join)
- LINQ: Help with linq query and contains for an IEnumerable<string>?
- Help rewriting existing TSQL query with "Not Exists" clause into LINQ
- how to write a Linq query with a EF code first Many to Many relationship
- Help with this Linq query
- Help with linq query
- Need help with a Linq XML conditional grouping query
- Need help with LINQ query
- help me with the linq query syntax to read elements not in a range
- Need help with a simple Linq query running with Entity Framework 4
- Help with a C# linq query on entity framework objects
- Linq query with join - help
- Help with a LINQ Query
- Help with this LINQ to SQL query
- help with linq query
- Help with writing a linq query
- Proper Linq Query for objects with many to many relation ship generated with code first entity framework
- help with linq to sql many to many query
- conditional where in linq query
- c# Parallel foreach with Lambda Expression to filter files
- How do I get all the elements and if they are assigned?
- Access any data that is not contained in grouped element
- getting values of DataTable column and putting it into new datatable C# using LINQ
- c# filtering list of list of integers if empty
- using If statement inside where clause in LINQ
- How do you remove OrderBy expression from an ExpressionTree using a ExpressionVisitor?
- Linq join three tables orderd by Timstamp
- Datatable Grouping Using Linq and VB .NET
- Unique Sub-JSon object of a Json object in C#
- Updating a property within a list where another property condition is met
- Why XDocument isn't reading elements value?
- How to order by a date part?
- Subtract a generic list from another
- Entity Framework not binding entity
- Access Linq Output in MVC Controller itself
- Calculate the difference from the previous item with LINQ
- LINQ, Polymorphism, MetaDataMapping, Inheritance Mapper
- Dynamic FirstOrDefault Predicate expression