more linq questions

D

David

Sorry about all these linq questions I am raising. There is so much about
this that I don't know and am getting frustrated with it. :-(

This has taken me ages to work out... (the structure of this code, just
simply to populate drop down lists)

using (DataClassesDataContext context = new
DataClassesDataContext())
{
var Query = from ct in context.aspnet_Users select ct;

if (Query != null)
{
UsersDropDownList.DataSource = Query.Distinct().ToList();
UsersDropDownList.DataTextField = "UserName";
UsersDropDownList.DataValueField = "UserId";
UsersDropDownList.DataBind();
}

var Query2 = from ct in context.Sections select ct;

if (Query != null)
{
SectionDropDownList.DataSource = Query2.Distinct().ToList();
SectionDropDownList.DataTextField = "SectionName";
SectionDropDownList.DataValueField = "SectionID";
SectionDropDownList.DataBind();
}
}
}

I couldn't re-use the Query variable, so had to create another.
At the moment, I don't know how to combine two fields in the DB... for
example, say I had UserEmpNo and UserName in aspnet_Users, then I want to
display both of those in my drop down. In sql, it would be simple... sql =
"select UserEmpNo + ' : ' + UserName as userDetail from aspnet_Users.

So, in the var Query above, I would need to do something like... (but I
can't have commas)

from ct in context.aspnet_Users select ct.UserEmpNo + ' : ' + UserName as
userDetail, UserId;

Any ideas how I would do this?

Also, if I wanted to use a linked table, say I had aspnet_Membership (which
is linked one to one to aspnet_Users) and then I linked aspnet_Membership
with a one to one to my own userDetails table, how could I get any items out
of my userDetails that relate to the intial aspnet_Users?

Thanks for your help.
--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
 
M

miher

David said:
Sorry about all these linq questions I am raising. There is so much about
this that I don't know and am getting frustrated with it. :-(

This has taken me ages to work out... (the structure of this code, just
simply to populate drop down lists)

using (DataClassesDataContext context = new
DataClassesDataContext())
{
var Query = from ct in context.aspnet_Users select ct;

if (Query != null)
{
UsersDropDownList.DataSource = Query.Distinct().ToList();
UsersDropDownList.DataTextField = "UserName";
UsersDropDownList.DataValueField = "UserId";
UsersDropDownList.DataBind();
}

var Query2 = from ct in context.Sections select ct;

if (Query != null)
{
SectionDropDownList.DataSource =
Query2.Distinct().ToList();
SectionDropDownList.DataTextField = "SectionName";
SectionDropDownList.DataValueField = "SectionID";
SectionDropDownList.DataBind();
}
}
}

I couldn't re-use the Query variable, so had to create another.
At the moment, I don't know how to combine two fields in the DB... for
example, say I had UserEmpNo and UserName in aspnet_Users, then I want to
display both of those in my drop down. In sql, it would be simple... sql =
"select UserEmpNo + ' : ' + UserName as userDetail from aspnet_Users.

So, in the var Query above, I would need to do something like... (but I
can't have commas)

from ct in context.aspnet_Users select ct.UserEmpNo + ' : ' + UserName as
userDetail, UserId;

Any ideas how I would do this?

Also, if I wanted to use a linked table, say I had aspnet_Membership
(which is linked one to one to aspnet_Users) and then I linked
aspnet_Membership with a one to one to my own userDetails table, how could
I get any items out of my userDetails that relate to the intial
aspnet_Users?

Thanks for your help.
--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available

Hi,

After select You can specify what kind of projection You would like to do,
so You can write something like this :
// assuming that UserEmpNo and UserName are properties You would like to
aggregate
var result = from ct in context.aspnet_Users
select new
{
UserDetail = string.Format("{0}:{1}",
ct.UserEmpNo, ct.UserName)
// whatever else properties You would like
to use
};

(Some videos about Linq2Sql :
http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2007/05/10/9322.aspx )

Hope You find this useful.
-Zsolt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top