Wrox Programmer Forums
|
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
 
Old September 9th, 2015, 11:06 AM
Authorized User
 
Join Date: Nov 2010
Posts: 63
Thanks: 3
Thanked 0 Times in 0 Posts
Default Entity

Hi
I would like to retrieve the number of records of a table (users) in an entity, depending on a certain result. I tried
Using myentities As New entity
If x=1 then
Dim users = (From r In myentities.users).Count 'Get number of all users
else
Dim users = (From r In myentities.users where r.name=N'Brown').Count 'Get number of users named Brown
end if
msgbox(users).

But it doesn't work, the Dim... cannot be in an if/endif
How can i solve this?
Thanks
Alex
 
Old September 9th, 2015, 02:08 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You can use Dim within an If statement. However, the variable is scoped to that block.

Instead, you can declare the variable outside the block and then use it inside and after:

Code:
Dim users As Integer

Using myentities As New entity
If x=1 then
      users = (From r In myentities.users).Count 'Get number of all users
else
      users = (From r In myentities.users where r.name=N'Brown').Count 'Get number of users named Brown
end if
msgbox(users)
Note that although MsgBox *seems* to appear to work in a development environment, it won't work for your web users.

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!

Last edited by Imar; September 9th, 2015 at 02:11 PM..
 
Old September 9th, 2015, 03:06 PM
Authorized User
 
Join Date: Nov 2010
Posts: 63
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Fantastic. So simple and so clear. Learned a lot. Thanks.
Alex
 
Old September 9th, 2015, 03:29 PM
Authorized User
 
Join Date: Nov 2010
Posts: 63
Thanks: 3
Thanked 0 Times in 0 Posts
Default Another question

What type (for dim) is users in this example?
users = (From r In myentities.view_user).SingleOrDefault
 
Old September 10th, 2015, 03:24 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

So it is the same as the other thread: just hover over Dim or users and it'll show you the type; likely it'll be view_user but I can't tell for sure. It'll be a single instance (because of your SingleOrDefault) of whatever the underlying type of view_user is.

Note that this likely won't work:

(From r In myentities.view_user).SingleOrDefault

There's no restrictive Where clause (for example, finding one y its ID) so unless there's exactly one user in your database SingleOrDefault will crash because it'll encounter more than one object.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old September 10th, 2015, 02:46 PM
Authorized User
 
Join Date: Nov 2010
Posts: 63
Thanks: 3
Thanked 0 Times in 0 Posts
Default Found the solution....

Dim users As IQueryable(Of view_user)

Thanks for your help.
Alex





Similar Threads
Thread Thread Starter Forum Replies Last Post
using entity framework 5 misuk11 BOOK: Professional ASP.NET Design Patterns 1 February 25th, 2014 05:23 PM
Entity Framework [email protected] BOOK: Professional Sitecore Development 0 January 3rd, 2014 12:40 PM
character entity into numeric character entity srkumar XSLT 1 November 22nd, 2007 04:53 AM
Difference between Entity and Entity type arshad mahmood C++ Programming 0 May 8th, 2004 12:34 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.