Soldato
Hi all,
I am doing some basic entity framework sql querying at the minute but have a couple of questions.
1) How can I select fields dynamically?
i.e in SQL when you write:
SELECT FieldA, FieldB
FROM Table
At the moment I am declaring all the fields I want in my query, would be awesome if I could base the queried fields based on some user inputs/controls on my webform.
2) Filtering/Ordering on a field that is not in the select statement?
Right now you can see I am adding filters (a simple example of one). If I don't include the column in my select list it complains about it.
In SQL I could query like this:
SELECT FieldA, FieldB
FROM TABLE
WHERE FieldC = 'SomeString'
ORDER BY FieldD
Doesn't seem that simple here, but maybe I am missing something!
Full code example below.
Quick note: I am not a developer by trade, I learn enough to be able to do what I need to do really so I may not have all the knowledge I need, but I get by OK.
I am doing some basic entity framework sql querying at the minute but have a couple of questions.
1) How can I select fields dynamically?
i.e in SQL when you write:
SELECT FieldA, FieldB
FROM Table
At the moment I am declaring all the fields I want in my query, would be awesome if I could base the queried fields based on some user inputs/controls on my webform.
2) Filtering/Ordering on a field that is not in the select statement?
Right now you can see I am adding filters (a simple example of one). If I don't include the column in my select list it complains about it.
In SQL I could query like this:
SELECT FieldA, FieldB
FROM TABLE
WHERE FieldC = 'SomeString'
ORDER BY FieldD
Doesn't seem that simple here, but maybe I am missing something!
Full code example below.
Code:
using (var Context = new ProjectEntities())
{
// Base Query
var Query = from user in Context.Users
select new {
user.CompanyName,
user.Mail
};
// Add Filters
if (!(string.IsNullOrWhiteSpace(SearchCompanyNameTextBox.Text)))
{
Query = Query.Where(u => u.CompanyName.StartsWith(SearchCompanyNameTextBox.Text));
}
// Order the Query
Query = Query
.OrderBy(u => u.Mail);
// Set the Record Count
GlobalVars.TotalRecords = Query.Count();
// Add Paging
Query = Query
.Skip(GlobalVars.Skip)
.Take(GlobalVars.Take);
// GridView Datasource Binding
GridViewResults.DataSource = Query;
GridViewResults.DataBind();
// Show/Hide Columns
SetColumns(GridViewResults);
}
Quick note: I am not a developer by trade, I learn enough to be able to do what I need to do really so I may not have all the knowledge I need, but I get by OK.
Last edited: