LINQ-to-sql + C#. Only retrieve some columns from table to gridview
I try to pull some data and insert it into a gridview. But I must be doing something wrong since I can only select a single column. If I try to get both first and lastname then they will just be inserted into the same td in the gridview.
Method so far is:
public string[] ShowName()
{
LinqToEbuboo_20DataContext db = new LinqToEbuboo_20DataContext();
var myusers = from u in db.db_users
where u.uid > 13
select u.firstname;
return myusers.ToArray();
}
I am fairly unfamiliar with both C# and LINQ (and gridviews for that sake) so it is probably a fairly simple problem.
It is build like this:
1) aspx (frontend)
2) webservice
3) C# (business logic)
4) database
The method posted here is in Business Layer and will be called from the webservive that will return the result to the frontend where the result will be bound to a gridview like this:
GridView1.DataSource = s.showemail();
GridView1.DataBind();
----------(end of original question)--------------------
Follow-up on answers: This is what I have build now based on input. It still does not work. From aspx where I try to set the datasource for the gridview an error will show. Something with a reference that is needed. But when trying to create it I cannot due to a loop in references...
user.cs
namespace Ebuboo
{
public class User
{
public PersonName[] ListStrangers()
{
LinqToEbuboo_20DataContext db = new LinqToEbuboo_20DataContext();
var myusers = from u in db.db_users
where u.uid > 12
select new PersonName { FirstNa开发者_JAVA技巧me = u.firstname, LastName = u.lastname };
return myusers.ToArray();
}
}
}
public class PersonName
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
The ListStrangers() is called from a webservice:
[WebMethod]
public PersonName[] ws_listUsers()
{
User u = new User();
return u.ListStrangers();
}
And the webservice is called from an aspx that try to bind the result to a gridview:
// create instance of webservice
Ebuboo_WS.Service1 s = new Ebuboo_WS.Service1();
// Bind and output result in Gridview
GvListUsers.DataSource = s.ws_listUsers();
GvListUsers.DataBind();
Br. Anders
You can't return an anonymous type from your function so you can't get multiple columns that way.
You would need to do something like this where you create a type that holds the results of your query (the multiple columns) so that you can return them from a function.
public PersonName[] ShowName()
{
LinqToEbuboo_20DataContext db = new LinqToEbuboo_20DataContext();
var myusers = from u in db.db_users
where u.uid > 13
select new PersonName { FirstName = u.firstname, LastName = u.lastname };
return myusers.ToArray();
}
public class PersonName {
public string FirstName { get; set; };
public string LastName { get; set; };
}
Try to create an Anonymous Type in your query:
LinqToEbuboo_20DataContext db = new LinqToEbuboo_20DataContext();
var myusers = from u in db.db_users
where u.uid > 13
select new { u.firstname, u.lastname };
However, as Jason Punyon wrote, you can't return Anonymous Types from your method.
精彩评论