• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

How do I separate the values in my query or .ToList() into variables I can access in Entity?

Below I can show the record in a gridView but how do i parse the record field and move them to variables for manipulation?  When i try to put them in a label i get a string of long string of characters but not the value.

string F = "Female";
string L = "licensed";
var HR = OE.tbl_Dept.Where(d => d.Gender.Equals(F) && d.Licensed.Equals(L) );

DeptGrid.DataSource = HR.ToList();
DeptGrid.DataBind();

Open in new window

0
bmanmike39
Asked:
bmanmike39
  • 4
  • 3
1 Solution
 
Fernando SotoCommented:
Where are you trying to access the fields from, DataGrid or the original results from HR. It would also help to see the code you are using.
0
 
bmanmike39Author Commented:
Preferably from the original HR
0
 
Fernando SotoCommented:
Hi bmanmike39;

First I would return a List<tbl_Dept> from the query into HR variable as shown below.

string F = "Female";
string L = "licensed";
var HR = OE.tbl_Dept.Where(d => d.Gender.Equals(F) && d.Licensed.Equals(L) ).ToList();

// Set DataSource as HR seeming it is already in a List object.
DeptGrid.DataSource = HR;
DeptGrid.DataBind();

Open in new window

Now to go through the List<tbl_Dept> and get the values to use in your code you can do the following. Remember that the List<tbl_Dept> is a collection of tbl_Dept objects and so putting the values into a control must be able to display all the values you need to display. For example a Label control can only be assigned on text value so you would need to build a string with new line characters after each entry as shown below.
StringBuilder sb = new StringBuilder();
foreach( tbl_Dept d in HR )
{
    // You access the different fields of d by using the column name used in the database
    // as shown here assuming that the table tbl_Dept has a column called DepartmentName
    sb.Append( d.DepartmentName + "\n" );
}

label1.Text = sb.ToString();

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
bmanmike39Author Commented:
Thank you!  But I'm still have the problem manipulating each piece of data in the record individually.  the string builder allows me to display it all in a label, but i want to separate it into multiple variables.
0
 
Fernando SotoCommented:
In the foreach loop the d is a row from the database table, in this case a row from the tbl_Dept,  with all it columns contain in it. To access any of the properties with in d just enter d.ColumnName and assign it to some variable of the same type as that of the column. For example lets say that the table has a column name DeptNumber and in the database table it is an int then to assign that to a variable in your code called deptNum you would the following.

int deptNum = d.DeptNumber;

If you are still having issues please post the code and any exceptions messages and I will have a look at it.
0
 
bmanmike39Author Commented:
Excellent!  Thanks,  you have help me with my understanding of how this works greatly!
0
 
Fernando SotoCommented:
Not a problem bmanmike39, glad to be of help.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now