Return a List<string> with a linq compiled query

414 Views Asked by At

I'm creating a compiled linq to sql query based on the tutorial here and I'm trying to figure out how to return the results as a list so I can use the results in my code. Currently it is the default IEnumerable and I get the error that it can't be enumerated twice.

public static Func<OoplesDBDataContext, string, IEnumerable<string>> GetValidSymbols
    {
        get
        {
            Func<OoplesDBDataContext, string, IEnumerable<string>> func =
                CompiledQuery.Compile<OoplesDBDataContext, string, IEnumerable<string>>
                ((OoplesDBDataContext context, string market) =>
                from c in context.Symbols
                where c.Market == market && c.isActive == true && c.isUnderReview == false
                select c.Symbol1);
            return func;
        }
    }

public static List<string> getStockSymbols(string market)
    {
        List<string> symbolList = new List<string>();

        try
        {
            using (OoplesDBDataContext context = new OoplesDBDataContext())
            {
                context.ObjectTrackingEnabled = false;
                var query = QueriesUtility.GetValidSymbols(context, market);

                return query.ToList(); // breaks here with the error
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        return null;
    }
0

There are 0 best solutions below