Selected value from dropdownlist has to be tablename from linq query - asp.net

I have a problem with my linq query. I want a search field (textbox) with a dropdownlist next to it. When i set the dropdownlist on "ProductID" he has to search only in the table "ProductID", And when i put it on "Productinformation", he has to search in the table "productinformation", i hope somebody understand this?
So what i want is the following query:
var textboxvalue = TextBox1.Text;
var dropdownsearch = DropDownList1.SelectedValue;
var Metadata = from m in db.Metadatas
join mm in db.Multimedias
on m.multimediaID equals mm.multimediaID
where (m. {{{Here i want the dropdownsearch}}} .ToString().Contains(textboxvalue) ||
mm. {{{Here i want the dropdownsearch}}} .ToString().Contains(textboxvalue))
select new
{
mm.ProductID,
mm.filename,
mm.filetype,
mm.filesize
};
So, how can i get the selected value from the dropdownlist, as a table in the query? Normally you would put m.ProductID into the query, but i want the Selected value in it, something like m.(Dropdownlist1.Selectedvalue)... or m.dropdownsearch..
Is that possible? And how?
Thanks :)

var Metadata = from m in db.Metadatas
join mm in db.Multimedias
on m.multimediaID equals mm.multimediaID
select new { m, mm };
var filtered = Metadata.Where("m." + dropdownsearch + " like '#0'", textboxvalue);
var filtered = Metadata.Where("mm." + dropdownsearch + " like '#0'", textboxvalue);
var result = filtered.Select(f => new
{
f.mm.ProductID,
f.mm.filename,
f.mm.filetype,
f.mm.filesize
};

Related

Search using linq and dropdowns

I got about 5 look-a-like linq querys just like this SortPerson() metod. I'm trying to develop a search using dropdowns where a user can select values from the dropdown and returns the values that are true from one or more dropdowns the user has selected to use.
Is there a simpler way to develop this? help would be much appreciated
public void SortPerson()
{
var personId = ddlPerson.SelectedValue;
var data = new MyModelContext();
var documents = from d in data.tblDocuments
join sp in data.tblPersons on d.DocPerson equals sp.PersonId
select d;
if (!String.IsNullOrEmpty(personId))
{
documents = documents.Where(c => c.DocPerson.Equals(personId));
}
rptResult.DataSource = documents.ToList();
rptResult.DataBind();
}
I don't see the point in joining without Where if you still select only one table.
If you want all document in case when Person is not selected, then you can't create much simpler method. You can write it shorter like:
var documents =
from d in data.tblDocuments
join ...
where String.IsNullOrEmpty(personId) || d.DocPerson equals personId
select d;
so you don't need separate if statement.
If you want to use several values from 5 dropdowns and use them as conditions in single query, just add more conditions:
var personId = ddlPerson.SelectedValue;
var someValue = ddlSomeDDL.SelectedValue;
//3 more values from DDL
var documents = from d in data.tblDocuments
join sp in data.tblPersons on d.DocPerson equals sp.PersonId
where (String.IsNullOrEmpty(personId) || sp.PersonId equals personId)
&& (String.IsNullOrEmpty(someValue) || d.SomeColumn equals someValue)
//3 more conditions
select d;

Displaying LINQ query results in a DataGridView with a DataTable

I have a function which contains a LINQ query and returns the results as a DataTable. I'm having trouble using the returned DataTable to populate a DataGridView. The query should be returning some data, but the table looks to be coming back empty. My function is as follows:
public static DataTable GetEmployeeTimesheets(int employeeId)
{
DataTable table = new DataTable();
using (PTMS_DataEntities entities = new PTMS_DataEntities())
{
var timesheets = from timesheet in entities.Timesheets
join timesheetTask in entities.Timesheet_Task
on timesheet.Id equals timesheetTask.Timesheet_Id
join task in entities.Tasks
on timesheetTask.Task_Id equals task.Id
join project in entities.Projects
on task.Project_Id equals project.Id
join department in entities.Departments
on project.Department_Id equals department.Id
where timesheet.Employee_Id == employeeId
select new
{
date = timesheet.Date,
taskName = task.Name,
projectName = project.Name,
projectDesc = project.Description,
departmentName = department.Name,
taskEstimatedHours = task.Estimated_Hours,
timesheetHours = timesheetTask.Hours
};
table.Columns.Add("date");
table.Columns.Add("taskName");
table.Columns.Add("projectName");
table.Columns.Add("projectDesc");
table.Columns.Add("departmentName");
table.Columns.Add("taskEstimatedHours");
table.Columns.Add("timesheetHours");
foreach (var item in timesheets)
{
table.Rows.Add(item.date, item.taskName, item.projectName,
item.projectDesc, item.departmentName, item.taskEstimatedHours,
item.timesheetHours);
}
}
return table;
}
The code populating the DataGridView is as follows:
TimesheetGrid.DataSource = EmployeeManager.GetEmployeeTimesheets(employeeId);
TimesheetGrid.DataBind();
Am I doing something wrong in the function / LINQ query itself, or am I just not populating the DataGridView correctly?

DropDownList value into Linq query where clause

I am facing following problem. I want a search field with a DropDowList next to it, where user can pick an item for which he wants to search. I have to make it with this LINQ code not just a SQL query.
Here's my code:
var Metadata = from m in db.Metadatas
join mm in db.Multimedias
on m.multimediaID equals mm.multimediaID
where (m. { Here would i have the selected value from the dropdownlist. } .ToString().Contains(textboxvalue) ||
mm. { Here would i have the selected value from the dropdownlist. } .ToString().Contains(textboxvalue))
&& mm.filetype.ToString().Contains(radiobuttonvalue)
I want to put something like: "Dropdownlist.selectedvalue" into the area { Here would i have the selected value from the dropdownlist. }
I hope you guys understand my idea and problem.
I am not sure why you are doing join on in your linq statement. That is not necessary if you Linq already knows about the relationship between the objects because there is a foreign key relationship setup in the database.
What you can do is this:
var Metedata = db.Metadatas;
switch(Dropdownlist.selectedvalue)
{
case "one":
Metadata = Metadata.Where(m => m.{selected value field}.Contains(textboxvalue));
break;
case "two":
Metadata = Metadata.Where(m => m.{selected value field}.Contains(textboxvalue));
break;
//More use cases
}
I am not sure what you will be selecting out of the list when you are done, but if it involves related objects (e.g. Multimedias) then you might want to look into DataLoadOptions (LinqToSQL) or .Include() (EntityFrameworks).
Just change your sql query:
if(supportgrp.SelectedItem.Text == "All")
sql ="SELECT * FROM QlyData where Date >='" + txtstartdate.Text + "' and Date<='" + txtenddate.Text + "'";
else
sql ="SELECT * FROM QlyData where Date >='" + txtstartdate.Text + "' and Date<='" + txtenddate.Text + "' and suppgrp = '" + supportgrp.Text + "'";

how to group a query in linq to Entity

I am using linq to Entity to retrieve data from to different tables by joining them, but I also want to group them by the field problemDesc in order to get rid of unnecessary duplicate entries for the same problem.
here is the code:
using (AssistantEntities context = new AssistantEntities())
{
var problems = context.tblProblems;
var customers = context.tblCustomers;
var query =
from problem in problems
join customer in customers
on problem.CustID equals customer.custID
where problem.IsActive == true
orderby customer.isMonthlyService == true descending
select new
{
problemID = problem.problemID,
ProblemCreateDate = problem.ProblemCreateDate,
CustID = problem.CustID,
name = customer.name,
isMonthlyService = customer.isMonthlyService,
StationName = problem.StationName,
problemDesc = problem.problemDesc,
LogMeIn = problem.LogMeIn
};
return query.ToList();
}
I am doing query.toList() in order to use that list in a gridview as a dataSource.
and if it possible, also add a field that count the duplicate problems.
You have plenty of examples in the following link.
LINQ - Grouping Operators

DataGrid view in asp.net is not displaying data

I want to dispaly column in datagrid view using custom query statement
but i want to send value to parametrized data in query
how can i do this ?
my code is below
select
c.customer_id,
c.customer_first_name,
c.customer_last_name,
c.customer_address,
c.account_number,
c.account_type,
a.account_balance,
t.transfer_amount_balance,
t.samebank_transfer_id,
t.account_number_same_bank,
t.transferdatetime
FROM customer_details c join account_details a on c.account_number = a.account_number
join transactions_details t on a.account_number = t.account_number where
c.customer_id = 'cus0010' and a.account_number = 'acc0010'
this above code working properly in sql server 2005
but the code below which is modified as per asp.net page for grid view is not showing
any result
select
c.customer_id,
c.customer_first_name,
c.customer_last_name,
c.customer_address,
c.account_number,
c.account_type,
a.account_balance,
t.transfer_amount_balance,
t.samebank_transfer_id,
t.account_number_same_bank,
t.transferdatetime
FROM customer_details c join account_details a on c.account_number = a.account_number
join transactions_details t on a.account_number = t.account_number where
c.customer_id = 'Label1.Text' and a.account_number = 'Label2.Text'
the above is placed in my custom sql query section it
is triggered by button click in my asp page or any other
idea to display it will be welcomed
Use:
string.Format("c.customer_id = '{0}' and a.account_number = '{1}'", Label1.Text, Label2.Text);
Consider this query:
string query = "insert into TestTable (Column1, Column2) values (#p1, #p2)";
p1 & p2 are parameters, in order to set the value for the parameters you need to use:
queryParameters[0] = new SqlCeParameter("p1", SqlDbType.NVarChar);
queryParameters[0].Value = Label1.Text;
queryParameters[1] = new SqlCeParameter("p2", SqlDbType.NVarChar);
queryParameters[1].Value = Label2.Text;
SqlCeCommand command = new SqlCeCommand(query);
command.Parameters.AddRange(queryParameters);
When the wizard is generating the query you need to use place holders/parameters for customer_ID and account_number and set their values by using parameters.
Edit:
In order to make the wizard create a parameter to use in the query, add a ? in the filter column in the query builder wizard.
Well, I may misunderstand, but...you are not actually sending the string 'Label1.Text' I guess? You should send the value of the textbox, something like this (if you are building the SQL as a string?):
...[SQL]... + "c.customer_id = '"+ Label1.Text + "'" ...[rest of the SQL]

Resources