score:3
tables.AddRange(dbServer.Tables
.Where(t => !t.IsSet)
.SelectMany(t => table.References)
.Where(r => r.PrimaryTable == "myTable"));
score:4
EDIT
Actually, I'm a bit confused by this code. Are you sure it's doing what it's meant to do? In particular, what's throwing me off is the fact that you're enumerating over table.References
, but then, when a certain condition holds for a particular Reference
(i.e., refer.PrimaryTable == "myTable"
), you're adding the Table
(table
) instead of the Reference
(refer
).
What this means is that if a Table
has multiple Reference
objects with PrimaryTable == "myTable"
, your tables
collection might contain multiple copies of this Table
. Is this correct?
I'm going to go out on a limb and guess that what you really want to check is simply that a Table
has, in its References
collection, any Reference
object with PrimaryTable == "myTable"
. If that's the case, in your original code after tables.Add(table)
I would have simply added break
to avoid duplicates. (It may be that only one Reference
in each collection would ever have the same PrimaryTable
, which case you'd be fine; but you could still stop enumerating at this point. Unless of course you want the duplicates.)
In any case, Lee's code (and what I have below) is not duplicating this behavior. Rather, it's adding the Reference
objects to a list (because the final ToList
call is on an IEnumerable<Reference>
).
It seems like, if what I've described above is the behavior you're after, you might want to do this:
var tables = dbServer.Tables
.Where(table => !table.IsSet)
.Where(
table => table.References.Any(refer => refer.PrimaryTable == "myTable")
).ToList();
ORIGINAL ANSWER
I'm going to expand on Lee's answer. Let's analyze it line by line.
// 1. enumerating over a collection
foreach (Table table in dbServer.Tables)
{
// 2. checking a condition
if (!table.IsSet)
{
// 3. enumerating over another collection
foreach (Reference refer in table.References)
{
// 4. checking a condition
if (refer.PrimaryTable == "myTable")
{
// 5. adding to a collection
tables.Add(table);
}
}
}
}
OK. So we've got:
- Enumeration - simple -- that's where we start
- Condition checking - we'll need a
Where
- Enumeration over another collection -
SelectMany
- Condition checking -
Where
again - Adding - most likely
ToList
(depends on what type of collection you want)
Here's what it comes out to:
var tables = dbServer.Tables // step 1
.Where(table => !table.IsSet) // step 2
.SelectMany(table => table.References) // step 3
.Where(refer => refer.PrimaryTable == "myTable") // step 4
.ToList(); // step 5
Make sense?
score:5
Just need to use two from's
var q = from table in dbServer.Tables
where !table.IsSet
from refer in table.References
where refer.PrimaryTable == "myTable"
select table;
score:18
var tables = dbServer.Tables
.Where(t => !t.IsSet)
.SelectMany(t => t.References)
.Where(r => r.PrimaryTable == "myTable")
.ToList();
Assuming tables is a List<T>
EDIT: As the comment points out, this isn't the same as the original - it looks like what you actually want is this:
var tables = dbServer.Tables
.Where(t => !t.IsSet && t.References.Any(r => r.PrimaryTable == "myTable"))
.ToList();
This will give you all the tables which have a reference whose PrimaryTable is 'myTable' which assumes that there will only be one matching reference table. Otherwise you could have the same table added multiple times.
Source: stackoverflow.com
Related Query
- How to convert this foreach loop into Linq code?
- How do I convert this loop into a LINQ expression?
- How can i convert this code snippet into LINQ format?
- How to convert the following foreach loop to linq code format?
- How to combine this foreach loop into a one line lambda / linq
- How to convert foreach loop with a switch statement into LINQ
- How do I convert Foreach statement into linq expression?
- How can I convert this SQL Query into LINQ (OVER (PARTITION BY Date))
- How can I combine this code into one or two LINQ queries?
- How do I convert this double foreach loop to just lambdas?
- How to convert foreach loop to a Linq query?
- I want to convert this foreach loop to a LINQ statement
- convert foreach loop to linq code
- How to convert Foreach loop to Linq (in datagridview)
- How do i convert this linq code to inline sql
- Linq Puzzle: How do I convert this foreach to linq syntax?
- How to convert this recursive code to Linq
- How to convert this complex SQL Query with Subqueries into LINQ
- Convert this LINQ code back to a Loop (or Why is this object sometimes null)
- Linq - how to convert this code to linq
- How can I convert Foreach loop with Linq expression OR AsEnumerable in C#.net?
- Can I can convert this C# code into some Linq code?
- How to convert this sample foreach into lambda expression?
- How to convert the foreach loop to linq
- C# LINQ Find List Inside Another List, Better way to code this than a foreach loop
- How to convert this TSQL into Linq Method approach
- How to Convert this Linq Query into Lambda Expression
- How can I convert foreach loop to LINQ lambda
- Convert the code from Foreach loop to Linq
- Can I convert Dictionary loop inside of a foreach loop into a LINQ statement
More Query from same tag
- How to select only string records with contains conditions using Linq
- Get the count variable outside the linq query context
- Parse XML string with LINQ, create new object from xml
- Linq query to return all quotes for a specific category
- Batch Update using LINQ
- Reading Text file with Linq with 2 patterns
- c# create method that accept lambda for list of field name
- Nullable Field and SQL Is Null Issue
- Sql YEAR(),MONTH() in Linq
- Combining multiple expressions (Expression<Func<T,bool>>) not working with variables. Why?
- LINQ Dynamic List
- Filtering Enteties in EF 6.0 with WPF and MySql
- Why do I get a different output from analogous Linq queries?
- linq selecting into custom object
- Using a member access lambda expression to parametrise a LINQ to SQL predicate
- LINQ - Where a list contains an element of another list
- How to right fold using LINQ?
- Getting selective tag values using LINQ
- EF5 code first migrations navigating inside nested hierarchy
- Linq to work with slightly complex selectlist
- LINQ To SQL Contains Case Sensitive Searching
- Dynamic list using array from anthor list
- .Net Framework DataTable.Select(String) method when the filter expression contains ' or "
- How do I get total Qty using one linq query?
- Remove duplicates while merging lists using Union in LINQ
- How to pass an external parameter to LINQ where clause in CRM
- Select Nodes with specific attribute using Linq
- LINQ query with DateTime column, groupby Date only with 2nd variable, count total entries for each subgroup
- System.MissingMemberException was unhandled by user code
- How to get depth of node