Search code examples
c#asp.net-mvc

Check result in controller and write to variable


I use ASP.NET with MVC.

In my controller I make a query and send it to my view. This works without any problems.

...
invoices = invoices.OrderBy(m => m.Datum).ThenBy(m => m.Rechnungsnummer);
return View(await invoices.ToListAsync());
        

I now also want to check in the controller before I call the view whether one or more data records were found. If only one record was found, I would like to read one value from this dataset and use it in the controller for further queries.

When debugging the application I see that actually only one record was found.

invoices --> Ergebnisansicht --> [0]

There I have also my desired field "orderId".

Is it possible to read this value and write it into a variable?


Solution

  • @Michal
    Thanks for your input. It put me on the right track.
    Found the right solution for me.

        if (invoices.Count() == 1)
            {
                qry_order = (int)invoices.FirstOrDefault().orderId;
            }