How to convert this foreach loop to Linq code?

I am new to Linq and I would like to change my old C # code to use Linq. The idea behind this code is to select all tables where it is not installed, and the PrimaryTable link fields are "myTable"


foreach (Table table in dbServer.Tables)
            {
                if (!table.IsSet)
                {
                    foreach (Reference refer in table.References)
                    {
                        if (refer.PrimaryTable == "myTable")
                        {
                            tables.Add(table);
                        }
                    }
                }
            }

After digging on the Internet, I have this code


var q = from table in dbServer.Tables
                    let refers = from refer in table.References
                                 where refer.PrimaryTable == "myTable"
                                 select refer.ForeignTable
                    where refers.Contains(table.Name)
                    select table;

But it doesn’t work at all, and I need your help to make it work.

Thanks in advance.

+3
source share
4 answers
var tables = dbServer.Tables
    .Where(t => !t.IsSet)
    .SelectMany(t => t.References)
    .Where(r => r.PrimaryTable == "myTable")
    .ToList();

Suggested Tables: List<T>

EDIT: As noted in the comment, this is not the same as the original - it looks like you really want:

var tables = dbServer.Tables
    .Where(t => !t.IsSet && t.References.Any(r => r.PrimaryTable == "myTable"))
    .ToList();

, , PrimaryTable "myTable", , . .

+18

        var q = from table in dbServer.Tables
                where !table.IsSet
                from refer in table.References
                where refer.PrimaryTable == "myTable"
                select table;
+5

, . , , ? , , , - , table.References, , Reference (.. refer.PrimaryTable == "myTable"), , Table (Table) Reference (refer).

, Table Reference PrimaryTable == "myTable", tables Table. ?

, , , , Table References Reference PrimaryTable == "myTable". , tables.Add(table) break, . ( , Reference PrimaryTable, , . , , .)

( , ) . , Reference ( ToList IEnumerable<Reference>).

, , , , , :

var tables = dbServer.Tables
    .Where(table => !table.IsSet)
    .Where(
        table => table.References.Any(refer => refer.PrimaryTable == "myTable")
    ).ToList();

ORIGINAL RESPONSE

I'm going to expand Lee's answer. 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 have:

  • Enumeration - just - where we start
  • Checking conditions - we will need Where
  • Listing over another collection - SelectMany
  • Status Check - WhereAgain
  • Adding - most likely ToList(depends on what type of collection you want)

Here is the result:

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?

+4
source
tables.AddRange(dbServer.Tables
    .Where(t => !t.IsSet)
    .SelectMany(t => table.References)
        .Where(r => r.PrimaryTable == "myTable"));
+3
source

Source: https://habr.com/ru/post/1743336/


All Articles