Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 9th, 2015, 11:06 AM
Authorized User
Points: 322, Level: 6
Points: 322, Level: 6 Points: 322, Level: 6 Points: 322, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
Reply With Quote
  #2 (permalink)  
Old September 9th, 2015, 02:08 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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..
Reply With Quote
  #3 (permalink)  
Old September 9th, 2015, 03:06 PM
Authorized User
Points: 322, Level: 6
Points: 322, Level: 6 Points: 322, Level: 6 Points: 322, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
Reply With Quote
  #4 (permalink)  
Old September 9th, 2015, 03:29 PM
Authorized User
Points: 322, Level: 6
Points: 322, Level: 6 Points: 322, Level: 6 Points: 322, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
Reply With Quote
  #5 (permalink)  
Old September 10th, 2015, 03:24 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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!
Reply With Quote
  #6 (permalink)  
Old September 10th, 2015, 02:46 PM
Authorized User
Points: 322, Level: 6
Points: 322, Level: 6 Points: 322, Level: 6 Points: 322, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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 baxterwlane@hotmail.com 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



All times are GMT -4. The time now is 01:12 PM.


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