0

I've tried the below query to get a list from a database using linq but get the following error

Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context.

I'm not sure why the error is being caused.

 List<a> a = await (from p in new db_Context().Items
                    where p.sub_Var.Name.Equals((sender as Button).Text)
                    orderby p.Name
                    select p).ToListAsync();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Max
  • 50
  • 7
  • make sure your sender actually is a Button Object every time this function gets called other than this I dont see where a null reference might comw from – D4rth B4n3 Oct 22 '17 at 16:36

2 Answers2

1

use == instead of Equals

 List<a> a = await (from p in new db_Context().Items
                    where p.sub_Var.Name == ((sender as Button).Text)
                    orderby p.Name
                    select p).ToListAsync();

EDIT

if you are getting null, set the button text to a variable and check ,

string buttonname = (sender as Button).Text;


List<a> a = await (from p in new db_Context().Items
                        where p.sub_Var.Name == buttonname 
                        orderby p.Name
                        select p).ToListAsync();
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
0
string name = (sender as Button).Text;

List<a> a = await (from p in new db_Context().Items
                    where p.sub_Var.Name == name 
                    orderby p.Name
                    select p).ToListAsync();
Interreto
  • 122
  • 1
  • 3