Solved

How to Declare Query before Assigning it?

Posted on 2010-09-19
2
473 Views
Last Modified: 2013-11-11
All the examples I've seen for LINQ declare and assign a query at the same time. However, I'd like to declare my query, and then, depending on a condition, build the query separately.

I assume the "gotcha" on this has to do with anonymous types, but it seems like there should be some way to assign the query an "anonymous" value.

What I'd like to do is the following (obviously, the "<enumerable type>" data type doesn't work, I think that's what needs fixing?)

    Dim dc As New UserDataContext
    Dim q As New <enumerable type>

    If CheckBox1.Checked = True Then
      q = From iterVar In dc.vwAllEmail _
              Select iterVar.Email

    Else
      q = From iterVar In dc.vwAllEmailType _
              Where iterVar.EmailType = intEmailType
              Select iterVar.Email
    End If

Open in new window

0
Comment
Question by:cdakz
2 Comments
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 500 total points
ID: 33712723
In your case it seems like the result is a collection of strings (assuming the Email field is a string), so the type is actually not anonymous. In that case you can declare the variable beforehand:

    Dim q As New IEnumerable(Of String)

If the result contains an anonymous type, you can't declare it with a specific type beforehand. You could declare the variable as Object, but then you would have problems using it later on.
0
 

Author Closing Comment

by:cdakz
ID: 33712765
That did the trick! Many thanks!
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now