Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Visual Studio.NET / IDE / April 2008

Tip: Looking for answers? Try searching our database.

How can we stored LINQ query definitions if var can only be local?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Juan Dent - 25 Apr 2008 21:47 GMT
HI,

the var "type" is great for holding a LINQ query definition, but it can only
be defined within a method. How can one store one or more LINQ queries in a
collection for example?

Signature

Thanks in advance,

Juan Dent, M.Sc.

Wen Yuan Wang [MSFT] - 28 Apr 2008 11:54 GMT
Hello Juan,

According to your description, you want to store one or more Linq Queries
in a Collection, and retrieve the query from Collecction in different
method, correct? If I misunderstood anything here, please don't hesitate to
correct me.

As you seen, one of the biggest limitation in "Var" type is that it cannot
be shared across methods. This has becomed a common issue in Linq project.
Wriju has a blog described the workaround for sharing "var" across method.
You may refer to the following article if you are interested in his
workaround.
http://blogs.msdn.com/wriju/archive/2007/12/19/sharing-var-across-the-method
.aspx
[Sharing "var" across the method ]

In your scenario, my suggestion is to insert each query into List<Object>,
and CastVar each object when querying. For example:

namespace ConsoleApplication16
{
   static class MyClass
   {
        public static IQueryable<T> CastVar<T>(this object obj, T anyType)
       {
           return (IQueryable<T>) obj;
       }
   }

   public class Class1
   {
       List<object> l = new List<object>();
       public Class1()
       {
           DataClasses1DataContext ddc = new DataClasses1DataContext();
           var q1 = from a in ddc.Table_1s
                   select new { c2=a.c2,c5=a.c5 };
           var q2 = from a in ddc.Table_1s
                    select new { c6 = a.c6 };
           l.Add(q1);
           l.Add(q2);
       }

       public string q1()
       {
           var q1 = l[0].CastVar(new {c2 = "", c5 = new bool?()});
           return q1.First().c2;
       }

       public decimal q2()
       {
           var q2 = l[1].CastVar(new { c6 = new decimal?() });
           return q2.First().c6.Value;
       }
   }
}

Hope this helps. Please try the above method and let me know if this is
what you need. We are glad to assist you.

Have a great day,
Best regards,
Wen Yuan
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my (L)manager at:
msdnmg@microsoft.com.
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.