English 中文(简体)
Linq Query Expression reuse
原标题:

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

问题回答

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.





相关问题
IEnumerable to array of parameter

Using linq? and XML is there a way to convert this IEnumerable to a string array of the value parameter? List<string> idList = new List<string>(); foreach (XElement idElement in word....

linq query for tag system - search for multiple tags

I have two tables, Tags(tagid, postid, tagname) and posts(postid, name, ...) now i want to make a query that returns me all posts that have a generic amount of tags. like: i want all posts that have ...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

How to filter duplicate list items

i have list of items IList with data that looks list this: GenId TestMode 1 0 1 1 3 0 3 1 4 NULL 2 NULL i want to remove the index ...

C# Grouping/Sorting a Generic List<> using LINQ

Im looking to group and sort a Generic List<>. I have a list of objects representing files and each of these objects has a FileName, FileType and FileDate property. FileType is defined as an ...

热门标签