Solved

How to Declare Query before Assigning it?

Posted on 2010-09-19
2
477 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

751 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