views:

216

answers:

1

Post Edited

Would this be possible ?

Have a query expression that is precompiled

i.e

private static Func<SmwrDataContext, int, IQueryable<Xyz>> _validXyzs = 
   CompiledQuery.Compile((Context context, int Id) => 
                                from xyz in _db.XYZs 
                                join abc in _db.ABCs on xyz.Id equals abc.Id  
                                where xyz.TypeId = id && xyz.Flag && abc.Flag  
                                select xyz);

I had initially declared this within the same repository was accessing it directly, had no problems in consuming it.

public List<MyItem> GetItemsForValueRange(int xyzTypeId, double floor, double ceiling)
{
    return (from xyx from _validXyzs (_db, xyzTypeId)
            join num from _db.numbers xyz.ID equals num.lettersId
            where 
                 num.Value >= floor && num.Value <= ceiling
                 num.Flag
            select new {
                         Name = xyz.Name, 
                         Value = num.Value
                       }).ToList();
}

Later refactored the static variable into a different class as the same query was being comsumed by multiple repositories,

Declaration post refactoring was as follows (_filteredXyzs) resides in the same class as the method making it available for comsumption.

Public static IQueryable<Xyz> GetValidXyzs(Context context, int xyzTypeId)
{
   return from _filteredXyzs(context, id);
}

Was consuming it post refactoring as [RepositoryName].GetValidXyzs within any particular query context, but end up with the following "System.StackOverflowException' occurred in System.Data.Linq.dll"

Xyz entity is based on the top with its availability being determined by the flags other types within master tables.

With Xyz being comsumed in many locations, i precompiled the query for better performance, just wanted to centralize this aspect to make it more maintenance friendly.

When i step through debugger static methods exits without any error, but fails during next step i.e joining and evaluation. so i'm a bit stumped on how to go about resolving this ?

I'm sorry for typos & any other incorrect inferences as my knowledge w.r.t c# and Linq is limited,

Ps: on a sidenote Linq2Action recommends a static field with a non-static method

Any help would be appreciated

A: 

Other than a few syntax oddities (in a query expression it's where instead of Where, and I don't know why you've got [Id]) I think that should be okay. Admittedly I don't have much experience with compiled queries, but it's definitely worth a shot. LINQ is designed to be composable, after all.

Have you tried it? If you have any problems, edit them into the question so we can try to address them.

EDIT: Responding to "this works when defined within the same class, but does not when defined in an external" it sounds like you're still trying to call it as if it were in the same class. It's a method, call it like you'd call any other method - with the type name if it's a static method, or via a reference otherwise.

Jon Skeet
sorry about the typos, it does work when the static declarations are within the same class, but does not when within an external.is there any other way out for making queries more re-usable ?
kalki