
Signature
Thanks in advance,
Juan Dent, M.Sc.
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.