locked
QueryableExtensions for more than one element which satisfies the condition. RRS feed

  • Question

  • User-729601932 posted

    I use the following code in my controller

    foreach (ProduktArtikel d in produkt.ProduktArtikel)
    {
        var artikellieferant = await _context.ArtikelLieferant
            .SingleOrDefaultAsync(m => m.ArtikelId == d.ArtikelId);
    }

    This works without any problems.
    But now we have some changes and it is possible that sometimes we have more than one element which satisfies the condition.
    This generates an error:
    InvalidOperationException: Enumerator failed to MoveNextAsync.

    Can anyone tell me how I can make this Query that it works for both condition (one or multiple results)?

    FYI:
    If i have several results, I´ll make the selection in the view.

    Regards Paul

    Tuesday, September 22, 2020 9:12 PM

Answers

  • User1312693872 posted

    Hi,Paul_412

    Can anyone tell me how I can make this Query that it works for both condition (one or multiple results)?

    If i have several results, I´ll make the selection in the view.

    You can refer to my demo ,only the ArtikelId in 'ArtikelLieferant' which matches the ArtikelId in 'collection' can be set in the list:

    public async Task<IActionResult> Index()
            {
                IEnumerable<ProduktArtikel> collection = new ProduktArtikel[]
                    {
                        new ProduktArtikel{ArtikelId=1 },
                        new ProduktArtikel{ArtikelId=2 },
                        new ProduktArtikel{ArtikelId=3 }
                    };
                var mymodel = new List<ArtikelLieferant>();
                List<List<ArtikelLieferant>> list = new List<List<ArtikelLieferant>>();
    
                foreach (ProduktArtikel d in collection)
                {
                    mymodel = await _context.ArtikelLieferant
                        .Where(m => m.ArtikelId == d.ArtikelId)
                        .ToListAsync();
                    list.Add(mymodel);
                }
                return View(list);
            }

    Result:

    Best Regards,

    Jerry Cai

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 23, 2020 8:40 AM

All replies

  • User-474980206 posted

    SingleOrDefault enforce the query return zero or one rows. If you want zero to two rows, you need use ToListAsync(). Then you need to check if there are over two rows.

    Wednesday, September 23, 2020 2:31 AM
  • User1312693872 posted

    Hi,Paul_412

    Can anyone tell me how I can make this Query that it works for both condition (one or multiple results)?

    If i have several results, I´ll make the selection in the view.

    You can refer to my demo ,only the ArtikelId in 'ArtikelLieferant' which matches the ArtikelId in 'collection' can be set in the list:

    public async Task<IActionResult> Index()
            {
                IEnumerable<ProduktArtikel> collection = new ProduktArtikel[]
                    {
                        new ProduktArtikel{ArtikelId=1 },
                        new ProduktArtikel{ArtikelId=2 },
                        new ProduktArtikel{ArtikelId=3 }
                    };
                var mymodel = new List<ArtikelLieferant>();
                List<List<ArtikelLieferant>> list = new List<List<ArtikelLieferant>>();
    
                foreach (ProduktArtikel d in collection)
                {
                    mymodel = await _context.ArtikelLieferant
                        .Where(m => m.ArtikelId == d.ArtikelId)
                        .ToListAsync();
                    list.Add(mymodel);
                }
                return View(list);
            }

    Result:

    Best Regards,

    Jerry Cai

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 23, 2020 8:40 AM
  • User-729601932 posted

    Hi Jerry,

    thank you.
    You gave me the right hint.
    The solution is:

    foreach (ProduktArtikel d in produkt.ProduktArtikel)
    {
       var artikellieferant = await _context.ArtikelLieferant
          .Where(m => m.ArtikelId == d.ArtikelId).ToListAsync();
    }

    return View(produkt);

    Wednesday, September 23, 2020 9:08 PM