user1095549
user1095549

Reputation: 1429

wpf and ef select part of a table with include

I have a table with many fields and I want to get only a few individual fields, I work with EF and I add another table to the query as follows

            var Test= ve.Folders.Include("Hosting")
                .Where(a => a.Collateral!= true)
                .AsEnumerable()
                .Select(p => new 
                {
                    id = p.Folder_Id,
                    name = p.Full_Name,
                    add = p.Address,
                    date1 = p.Collateral_Date,
                    sName = p.Hosting._Name
                })
                .ToArray();

But with the field (sName= p.Hosting._Name) that is associated with the second table without any value query not working Many attempts have been tried but without result (interesting when I ask without Select everything works well)

Thanks in advance for any help

Upvotes: 0

Views: 116

Answers (1)

Cyanfish
Cyanfish

Reputation: 4153

One thing to note is that, in this case, there's little benefit to the Select after the call to AsEnumerable, since all the data in the table is still queried from the database (not just the fields you specifiy).

If you want to avoid that, and only query those five fields, you can remove the AsEnumerable call. That means the Select will execute as part of the SQL query. This also means the Include is unnecessary, since the Select will query all of the data you want.

var Test= ve.Folders
            .Where(a => a.Collateral!= true)
            .Select(p => new 
            {
                id = p.Folder_Id,
                name = p.Full_Name,
                add = p.Address,
                date1 = p.Collateral_Date,
                sName = p.Hosting._Name
            })
            .ToArray();

Upvotes: 2

Related Questions