you cannot send the result with the IQueryable directly. you can send the result to back using array like below
public Array GetAllSubGroup ()
{
MyDataContext db = new MyDataContext ();
Var result = from SubGroup in db.SubGroups
join Group in db.Groups on
SubGroup.GroupID equals Group.id
select new
{
id = SubGroup.id,
GroupName = Group.Group_Name,
Full_Name = SubGroup.Full_Name,
};
return result.ToArray();
}
and you can directly assign the function result to the grid view
GridView1.DataSource = GetAllSubGroup ();
Dont forget to mark the post as answer which will helps you
do somthing like this....
public List<SubGroup> GetAllSubGroup ()
{
MyDataContext db = new MyDataContext ();
Var result = from SubGroup in db.SubGroups
join Group in db.Groups on
SubGroup.GroupID equals Group.id
select new
{
id = SubGroup.id,
GroupName = Group.Group_Name,
Full_Name = SubGroup.Full_Name,
};
List<SubGroup> objlistsg = new List<SubGroup>();
foreach (var C in query)
{
SubGroup Obj_sg = new SubGroup();
Obj_sg.Id = C.Id;
Obj_sg.GroupName = C.GroupName;
Obj_sg.FullName = C.Full_Name;
objlistsg .add(Obj_sg);
}
return objlistsg ;
}
public class SubGroup
{
int _Id;
public int Id { get { return _Id; } set { _Id = value; } }
string _GroupName ;
public string GroupName { get { return _GroupName ; } set { _GroupName = value; } }
string _FullName ;
public string FullName { get { return _FullName ; } set { _FullName = value; } }
}
koohbor323
Member
25 Points
24 Posts
problem with joining tables in Linq to SQL in methods
Jul 30, 2010 09:41 AM|LINK
Hi
I have a problem with joining tables in Linq to SQL in methods
I have a GridView to show subgroup information along with Group Name
Here is my code that works fine
MyDataContext db = new MyDataContext ();
Var result = from SubGroup in db.SubGroups
join Group in db.Groups on
SubGroup.GroupID equals Group.id
select new
{
id = SubGroup.id,
GroupName = Group.Group_Name,
Full_Name = SubGroup.Full_Name,
};
GridView1.DataSource = result;
GridView1.DataBind();
But I don't know how to handle it by a methot like below :
public IQueryable<SubGroup> GetAllSubGroup ()
{
MyDataContext db = new MyDataContext ();
Var result = from SubGroup in db.SubGroups
join Group in db.Groups on
SubGroup.GroupID equals Group.id
select new
{
id = SubGroup.id,
GroupName = Group.Group_Name,
Full_Name = SubGroup.Full_Name,
};
return result;
}
Is there any way to do this? I will very thankfull for your helps
Kittu1434424
Member
27 Points
13 Posts
Re: problem with joining tables in Linq to SQL in methods
Jul 30, 2010 11:24 AM|LINK
hi koohbor323,
you cannot send the result with the IQueryable directly. you can send the result to back using array like below
public Array GetAllSubGroup () { MyDataContext db = new MyDataContext (); Var result = from SubGroup in db.SubGroups join Group in db.Groups on SubGroup.GroupID equals Group.id select new { id = SubGroup.id, GroupName = Group.Group_Name, Full_Name = SubGroup.Full_Name, }; return result.ToArray(); }and you can directly assign the function result to the grid view
Dont forget to mark the post as answer which will helps you
linq 2 sql Linq asp.net 3.5 Array list
vishwaraj1
Contributor
3687 Points
861 Posts
Re: problem with joining tables in Linq to SQL in methods
Jul 30, 2010 11:26 AM|LINK
do somthing like this.... public List<SubGroup> GetAllSubGroup () { MyDataContext db = new MyDataContext (); Var result = from SubGroup in db.SubGroups join Group in db.Groups on SubGroup.GroupID equals Group.id select new { id = SubGroup.id, GroupName = Group.Group_Name, Full_Name = SubGroup.Full_Name, }; List<SubGroup> objlistsg = new List<SubGroup>(); foreach (var C in query) { SubGroup Obj_sg = new SubGroup(); Obj_sg.Id = C.Id; Obj_sg.GroupName = C.GroupName; Obj_sg.FullName = C.Full_Name; objlistsg .add(Obj_sg); } return objlistsg ; } public class SubGroup { int _Id; public int Id { get { return _Id; } set { _Id = value; } } string _GroupName ; public string GroupName { get { return _GroupName ; } set { _GroupName = value; } } string _FullName ; public string FullName { get { return _FullName ; } set { _FullName = value; } } }Vishwaraj Malik
Skype ID: vishwaraj.malik
VB to C# Converter
Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.