返回带有linq编译查询的List <string> </string>

时间:2014-12-11 02:33:22

标签: c# linq linq.compiledquery

我正在根据教程here创建一个编译的linq to sql查询,我正在试图弄清楚如何将结果作为列表返回,以便我可以在我的代码中使用结果。目前它是默认的IEnumerable,我得到的错误是它无法枚举两次。

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 个答案:

没有答案