 |
BOOK: Beginning ASP.NET 4.5.1 : in C# and VB
 | This is the forum to discuss the Wrox book Beginning ASP.NET 4.5.1: in C# and VB by Imar Spaanjaars; ISBN: 978-1-118-84677-3 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning ASP.NET 4.5.1 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|
|

April 3rd, 2018, 01:00 PM
|
|
Authorized User
|
|
Join Date: Mar 2017
Posts: 55
Thanks: 12
Thanked 0 Times in 0 Posts
|
|
Okay, I got it working THIS way:
Code:
var context = new HolyRollersEntities();
var lst = (from x in context.tb_Decode
where x.DecodeNumber == 4
select new { x.DecodeID, x.DecodeDescription }).ToList();
// var lst2 = context.tb_Decode.Select(x => new { x.DecodeID, x.DecodeDescription }).ToList();
ddl.DataSource = lst;
But I would still like to know how to shorten it using the one-liner I commented out (var lst2).
|
|

April 3rd, 2018, 01:19 PM
|
|
Authorized User
|
|
Join Date: Mar 2017
Posts: 55
Thanks: 12
Thanked 0 Times in 0 Posts
|
|
I've got this working and the Insert works too - except:
It doesn't populate the ArmorCategory field with the selection from the drop down list.
How do I bind the value that the user selects.
The DataValueField (DecodeID) is the one that I want inserted into the ArmorCategory field name - how does that work?
I can't say <%# Bind("DecodeID") %> because the field is called ArmorCategory in the tb_Equip_Armor table.
I can't say <%# Bind("ArmorCategory") %> because that's not the name of the DataValueField.
|
|

April 3rd, 2018, 01:37 PM
|
|
Authorized User
|
|
Join Date: Mar 2017
Posts: 55
Thanks: 12
Thanked 0 Times in 0 Posts
|
|
It's working now.
I used:
Code:
SelectedValue='<%# Bind("ArmorCategory") %>'
|
|

April 3rd, 2018, 01:43 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Excellent!
|
|

April 3rd, 2018, 03:09 PM
|
|
Authorized User
|
|
Join Date: Mar 2017
Posts: 55
Thanks: 12
Thanked 0 Times in 0 Posts
|
|
I appreciate all the help and feedback!
Could you tell me how I could've added a where clause to this statement below:
Code:
var lst2 = context.tb_Decode.Select(x => new { x.DecodeID, x.DecodeDescription }).ToList();
|
|

April 3rd, 2018, 03:30 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Just inject the Where before the Select:
var lst2 = context.tb_Decode.Where(x => x.Id > 22 && x.Approved).Select(x => new { x.DecodeID, x.DecodeDescription }).ToList();
That would bring back all approved items with an ID greater than 22 (I just made up the Id and Approved properties for this example).
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

April 3rd, 2018, 03:31 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
And you're welcome, BTW. Glad you like it!
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

April 3rd, 2018, 05:24 PM
|
|
Authorized User
|
|
Join Date: Mar 2017
Posts: 55
Thanks: 12
Thanked 0 Times in 0 Posts
|
|
That's what I was trying to do - but now I see my syntax error.
I was attempting something like
Code:
.Where(x.DecodeNumber == 4).Select(etc...)
And even tried
Code:
.Where(x=>DecodeNumber == 4).Select(etc...)
If ONLY I had tried
Code:
.Where(x => x.DecodeNumber == 4).Select(etc...)
It works! Thanks!
|
|

April 4th, 2018, 03:13 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
You may find the following link useful. I linked to it in my book, but it's easily overlooked: https://docs.microsoft.com/en-us/dot...syntax-in-linq
|
|
 |
|