Solved

How to Declare Query before Assigning it?

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

632 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