Solved

How to Declare Query before Assigning it?

Posted on 2010-09-19
2
474 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

839 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