0

The app allows users to search by different categories. I would like to use a switch case instead of a bunch of ifs. The lamba expression does not accept the variable as it does not exist in the invoice_Master list. Is there a way to be able to use a variable that will equal what would be in the table? Note - This is using Entity Framework

This code works

                string searchValue = "";

                switch (searchBy)
                {
                    case "PONumber":
                        searchValue = "INVCE_31";
                        break;
                    case "SerialNumber":
                        searchValue = "ORDID_31";
                        break;
                    default:
                        return PartialView("InvoiceList", invoice_Master);
                }
                var results = invoice_Master.Where(m => m.INVCE_31== search).ToList();
                return PartialView("InvoiceList", results);

But this is what I want

                string searchValue = "";

                switch (searchBy)
                {
                    case "PONumber":
                        searchValue = "INVCE_31";
                        break;
                    case "SerialNumber":
                        searchValue = "ORDID_31";
                        break;
                    default:
                        return PartialView("InvoiceList", invoice_Master);
                }
                var results = invoice_Master.Where(m => m.searchValue == search).ToList();
                return PartialView("InvoiceList", results);

The only difference between the two is the second to bottom line where the lamba expression uses a variable instead of a defined item from the list.

Austin V.
  • 79
  • 7
  • You want to retrieve a property's value based on a `string` value containing the name of that property. It seems that this question has already been asked and answered multiple times on this site. See duplicates. – Peter Duniho Oct 27 '20 at 21:50
  • 1
    I can't answer anymore, it is closed... but in your switch case, use a variable (adapt the type for invoice) `Func predicate` instead of `string searchValue`, and like that : `case "PONumber": predicate = m => m.INVCE_31 == search;` and other similar thing for the other case. Then, use `invoice_Master.Where(predicate)`. – Pac0 Oct 27 '20 at 21:52
  • @Peter Duniho - I'm new to C# and developing in general, which doesn't give me any excuses, but I am not able to understand how those duplicate answers would work with a lamba expression. Wasn't a single example where someone was using one. Asked a few developers I knew and they had no clue. Looks like I'm SOL – Austin V. Oct 27 '20 at 21:54
  • @PeterDuniho Although it looks like it's answering OP's question, if it's using Entity framework, in a usual ASP.NET setup, you definitely don't want to apply anything like reflection as in all the suggested duplicates. There should probably be other duplicates of OP's situations, I concede, but those ones don't answer the question properly (IMO). – Pac0 Oct 27 '20 at 21:55
  • @Pac0: _"question is closed for wrong reason"_ -- all due respect, **no it's not**. You may feel there's a _better_ answer out there, but given the question that was asked, the duplicates address it perfectly. And you can always feel free to add whatever "better answer" you like to any of the duplicates. Being "clear and answerable" is of no use at all if the question already has an answer on the site. – Peter Duniho Oct 27 '20 at 22:06
  • OP mentioned Entity Framework. I certainly respect and praise that you're an gold badge bearer in C#, hence with power of single-handedly closing a C# question for duplicate. Are you an expert in Entity Framework? Because in this case, if it's linq to sql, then these duplicate targets solutions **won't work** . You can call it a X/Y problem if you want, as a beginner OP has been able to express some need that _resemble_ invoking a variable named in a string (Y). The proper X question is how to adapt the predicate in a search in Entity framework that can span different fields. – Pac0 Oct 27 '20 at 22:17
  • 1
    @AustinV. check my first comment, the answer is there (though a bit squeezed, and to be exact I would need the type of the "Invoice_Master" entities – Pac0 Oct 27 '20 at 22:22
  • @Pac0 That was the answer. Thanks a bunch. None of the listed duplicate answers were even remotely close to the answer. Gotta love Stack Overflow – Austin V. Oct 27 '20 at 22:42

0 Answers0