Solved

How to Declare Query before Assigning it?

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
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.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

20 Experts available now in Live!

Get 1:1 Help Now