How to select the whole object, Lambda C#

Asked

Viewed 263 times

1

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

Like a: SELECT * FROM Somethings

Without having to specify how: SELECT ID, BLA, BLE FROM Algumacoisa

I already got 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 don’t know if this is the right way to do it, I would like the more experienced to help me :)

I thought that part I did:

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

That 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
  • Why are you using Join?

  • Because I want to select only Printers that have counters in Printercounter table.

  • You want to bring Printer, which has information in Printercounter? if yes post the two models and if possible the two tables!

  • Unfortunately I can’t show beyond that :/

  • If you can’t even put the relationship?

  • I need to know if it’s 1 to N? relationship you have no problem posting!

  • 1

    The ratio is 1 to 1.

Show 2 more comments

2 answers

3

You can write 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 select only one:

query.Join(this.systemDbContext.PrinterCounter, printer => printer.PrinterId, pc => pc.PrinterId, (printer, pc) => new { Printer = printer }).First() / FirstOrDefault() / Single() / SingleOrDefault()...
  • None of them will be possible, as there is no transformation from an anonymous object to Iqueryable.

  • When I give a Tolist(), he says that the value cannot be null.

1


If your relationship is 1 to 1 amid 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 that 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
  • Exactly what I needed, thank you very much!

  • @Victorhugoschmidt if the answer is accepted please

Browser other questions tagged

You are not signed in. Login or sign up in order to post.