How to convert this foreach loop into Linq code?

Posted by a-galkin on Stack Overflow See other posts from Stack Overflow or by a-galkin
Published on 2010-04-29T11:21:52Z Indexed on 2010/04/29 11:27 UTC
Read the original article Hit count: 289

Filed under:
|

I am new one with Linq and I would like to modify my old c# code to use Linq. The idea of this code to select all tables where it's not set and reference’s field PrimaryTable equal "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 in internet I have got 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 does not work at all and I need your help to make it works.

Thanks in advance.

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ