How to select the entire object, Lambda C #

1

I have a difficulty here in this lambda, I would like to know how I select the entire object without having to specify all its properties.

As if it were a: SELECT * FROM SomeCase

Without having to specify as: SELECT ID, BLA, BLE FROM Some

I already have a piece of how I want it:

var query = this.clientDbContext.Printers
                .Where(p => p.PlaceId == placeId);

        if (!getRemoved)
            query = query.Where(where => where.WasRemoved == false);

        if (onlyWithCounters)
            query = query
                .Join(this.systemDbContext.PrinterCounter, printer => printer.PrinterId, pc => pc.PrinterId, (printer, pc) => new { Printer = printer })
                .Select(select => select.Printer);

I do not know if this is the correct way to do it, I would like the more experienced ones to help me:)

I thought that in this part I did:

(printer, pc) => new { Printer = printer })

This would solve

(printer, pc) => printer)

To clarify, I want to simulate this:

.Join(this.systemDbContext.PrinterCounter, printer => printer.PrinterId, pc => pc.PrinterId, (printer, pc) => new { Printer = printer })
                .Select(select => select.Printer);

That:

SELECT * 
FROM [*banco do cliente*].[dbo].[Printers] AS printer 
JOIN [*banco principal*].[dbo].PrinterCounter AS cp ON printer.PrinterId = cp.PrinterId
    
asked by anonymous 02.08.2017 / 21:39

2 answers

1

If your relationship is 1 to 1 between Printers and PrinterCounter do so:

Classes:

public class Printer
{
    public int PrinterId { get; set; }
    public string Name { get; set; }
    public virtual PrinterCounter PrinterCounter { get; set; }
}

public class PrinterCounter
{
    public int PrinterId { get; set; }
    public string Name { get; set; }    
    public virtual Printer Printer { get; set; }
}

Code:

using (MyDbContext db = new MyDbContext())
{
    var result = db.Printers
             .Where(c => c.PrinterCounter != null)
             .ToList();
}

SQL generated by this expression:

SELECT
    [Extent1].[PrinterId] AS [PrinterId],
    [Extent1].[Name] AS [Name]
    FROM  [dbo].[Printer] AS [Extent1]
    INNER JOIN [dbo].[PrinterCounter] AS [Extent2] 
            ON [Extent1].[PrinterId] = [Extent2].[PrinterId]
    WHERE 1 = 1
    
02.08.2017 / 22:58
3

You can write the select at the end of the expression, as LINQ mentioned:

query.Join(this.systemDbContext.PrinterCounter, printer => printer.PrinterId, pc => pc.PrinterId, (printer, pc) => new { Printer = printer })
                .Select(select => select);

Or it depends on what you need, to select a list:

query.Join(this.systemDbContext.PrinterCounter, printer => printer.PrinterId, pc => pc.PrinterId, (printer, pc) => new { Printer = printer }).ToList();

Or just select one:

query.Join(this.systemDbContext.PrinterCounter, printer => printer.PrinterId, pc => pc.PrinterId, (printer, pc) => new { Printer = printer }).First() / FirstOrDefault() / Single() / SingleOrDefault()...
    
02.08.2017 / 21:44