简体   繁体   中英

Left Outer join Extension method linq

i would translate this queries with extension method also i would merge this queries in a single query with extension method

var variants = _ctx.Varianti.Where(i=>i.attivo==0);

var allProducts = await (from p in  _ctx.Articoli
                         where  p.cat==1
                         join v in variants on p.code equals v.code into gj
                         from articoli in gj.DefaultIfEmpty()
                         select new {
                           Codart = p.Codart,
                           Codvar = articoli.Codvar,
                        }).ToListAsync(); 

My classes

class Articolo{ public string Codart //key public double price }

class Variante{ public string Codart //key public string Codvar // key public int attivo }

I have to return products like so

Prod1-Variant1 Prod2-(no variant) prod3-Variant1 prod4-Variant1 prod4-Variant2 prod5-(no variant)

I should filters only variants with attivo==0 And all product without variant if they not have

The code works well but i need to optimize in single query to database and also with extension method

In T-Sql should be as so:

SELECT Codart,
       Codvar
  FROM dbo.Articoli
       LEFT OUTER JOIN dbo.Varianti
           ON dbo.Articoli.Codart = dbo.Varianti.Codart
 WHERE (Cat = 1)
   AND (attivo = 0)

I'm still not sure what's the problem. Here some example how to "left-outer-join" the products and variants and select a new object.

List<Articolo> products = new List<Articolo>()
{
    new Articolo() { Code = "1", price = 1 },
    new Articolo() { Code = "2", price = 1 },
    new Articolo() { Code = "3", price = 1 },
    new Articolo() { Code = "4", price = 1 },
    new Articolo() { Code = "5", price = 1 },
};

List<Variante> variants = new List<Variante>()
{
    new Variante() { Code = "1", attivo = 0, Codvar = "v1" },
    new Variante() { Code = "3", attivo = 0, Codvar = "v1" },
    new Variante() { Code = "4", attivo = 0, Codvar = "v1" },
    new Variante() { Code = "4", attivo = 0, Codvar = "v2" },
    new Variante() { Code = "5", attivo = 1, Codvar = "v2" },
};

var result = products // Our "Left"-List 
    .GroupJoin( // Join to a "one"-to-"null or many"-List
        variants.Where(v => v.attivo == 0), // Our "right"-List
        p => p.Code, // select key in left list
        v => v.Code, // select key in right list
        (p, v) => // for every product "p" we have a list of variants "v"
            v.Any() ? // do we have vriants for our product?
                v.Select(s =>  new // for every variant we build our new product
                {
                    Code = p.Code,
                    FirstVariant = s.Codvar,
                })
                : // if we got no variants, we build a "no variant"-product
                new[] { new {
                    Code = p.Code,
                    FirstVariant = "No Variant"
                } } // here we got a list of product-variants per product ("list of lists")
            ).SelectMany(s => s); // We want one list of all product variants

foreach (var item in result)
{
    Console.WriteLine("Code: {0}, FirstVar: {1}", item.Code, item.FirstVariant);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM