Reputation: 189
I use asp.net c# with webforms and I work with the dal+bll structure. This is my situation:
I have a starting page with 8 buttons, when I click on one of this buttons, I go to a page list.aspx.
On this page, there is data returned from the database with this LINQ code:
var result = (from b in dc.Businesses
select b).ToList();
return result;
This all works as it should. However, I want to make it so that if I click on button1, only the records with businessType "NighShop" are returned. When I click on button2, only records with idName "Bar" are returned etc. It would be stupid to make 8 different pages + DAL&BLL's for this.
So I figure the LINQ (in the DALBusiness.cs class layer) should be something like this:
var result = (from b in dc.Businesses where b.BusinessType == aVariableValue
select b).ToList();
return result;
And that variableValue can be altered whenever the user presses one of the 8 buttons. But I have no experience with this, so I don't know how to do this.
I tried to declare the var when I press on a button (on index page):
public void Button1_Click(object sender, EventArgs e)
{
var keuze = "Nachtwinkel";
}
But if I try:
public IList<Business> selectAll()
{
var result = (from b in dc.Businesses where b.BusinessType == keuze
select b).ToList();
return result;
}
On theDALBusiness.cs class layer, then it doesn't know the var keuze. I'm new to asp.net, so the answer might be really easy after all.
Upvotes: 1
Views: 3727
Reputation: 18843
If it does not know that variable try making a method instead that compares to the
anIdVariable
will something like this work it's the same thing as the Linq but in more of a Method format just as an example
public bool FilterByBusinessTypeID(dc.Business dcBusiness)
{
return dcBusiness.ID == anIdVariable;
}
var result= BusinessType.Where(FilterByBusinessTypeID).First();
Upvotes: 2
Reputation: 175
in the LINQ query it will evaluate references as typed. If you specify b.BusinessType == x
and you have a type or entity "BusinessType", you will need to pass the object reference (i.e. x has to be typed "BusinessType"). You can get around this by doing something like the following:
//this line in your OnClick function
keuze = "MyBusinessTypeName";
//this line in your data handler
var result = (from b in dc.Businesses where b.BusinessType.BusinessTypeName == keuze
select b).ToList();
As Erix says, the keuze variable needs to be accessible to both. Hope that helps.
Upvotes: 2
Reputation: 7105
Try this. You need to store your Keuze value somewhere accessible to SelecAll().
public void Button1_Click(object sender, EventArgs e)
{
Session["keuze"] = "Nachtwinkel";
}
public IList<Business> selectAll()
{
var result = (from b in dc.Businesses where b.BusinessType == Session["keuze"]
select b).ToList();
return result;
}
Upvotes: 3
Reputation: 3743
Are you meaning to just compare the Id's like:
var result = (from b in dc.Businesses where b.BusinessType.Id == anIdVariable
select b).ToList();
return result;
Upvotes: 1