LINQ-TO-SQL 查询不适用于 CSV 文件

LINQ-TO-SQL query not working with CSV file

我正在从我的程序中读取一个简单的 csv 文件。这是我的 csv 文件的样子:

NASDQ,O,
OTC,O,
NYSE,N,
TSE,T,

这是我读取 csv 文件的代码:

string csvFile = @"x:\tech\SQL_IntlPricing\ExchangeLookup.csv";
string[] csvLines = File.ReadAllLines(csvFile);

var csvValues = csvLines
    .Select(l => new { 
        Exchange = l.Split(',').First(), 
        Lookup = l.Split(',').Skip(1).First ()});

到目前为止,代码一切正常。我正在使用以下 LINQ 查询:

from comp in Companies
where !comp.Coverage_status.Contains("drop")
select new
{
    FSTick = string.Format("{0}-{1}", comp.Ticker,
                csvValues
                    .Where(v => v.Exchange.Contains(comp.Exchange))
                    .Select(v => v.Lookup).FirstOrDefault())
};

但我收到以下错误:

NotSupportedException: Local sequence cannot be used in LINQ to SQL implementations of query operators except the Contains operator.

基本上,我试图获得以下结果:

AAPL-O
MSFT-O

有没有办法让我使用 LINQ 查询获得我想要的结果?

如果公司不是很多,那么简单的解决方案是:

    from comp in Companies.Where(c => !c.Coverage_status.Contains("drop")).AsEnumerable()
select new
{
    FSTick = string.Format("{0}-{1}", comp.Ticker,
                csvValues
                    .Where(v => v.Exchange.Contains(comp.Exchange))
                    .Select(v => v.Lookup).FirstOrDefault())
};

否则你可以在那里进行过滤;

    from comp in Companies.Where( c => 
      csvValues.Select(cs => cs.Exchange).Contains(comp.Exchange) &&
      !c.Coverage_status.Contains("drop")
    ).AsEnumerable()
    select new
    {
        FSTick = string.Format("{0}-{1}", comp.Ticker,
                    csvValues
                        .Where(v => v.Exchange.Contains(comp.Exchange))
                        .Select(v => v.Lookup).FirstOrDefault())
    };

根据我上面的评论,如果将 linq-sql 表达式转换为其扩展方法语法形式没问题,您可以执行以下操作:

为了示例,我创建了一个公司列表。公司被定义为

   public class Company
    {
        public string Coverage_status { get; set; }
        public string Exchange { get; set; }
        public string Ticker { get; set; }
    }

下面是代码的完整示例:

 List<string> csvLines = new List<string>
    {
        "NASDQ,O,",
        "OTC,O,",
        "NYSE,N,",
        "TSE,T,"
    };

    var csvValues = csvLines
    .Select(l => new
    {
        Exchange = l.Split(',').First(),
        Lookup = l.Split(',').Skip(1).First()
    });

    List<Company> companies = new List<Company>
    {
        new Company { Coverage_status = "aaa", Ticker = "123", Exchange = "NASDQ"},
        new Company { Coverage_status = "1521drop422", Ticker = "1251223", Exchange = "aaaaaaaa"},
        new Company { Coverage_status = "f2hdjjd", Ticker = "15525221123", Exchange = "TSE"}
    };

    var result = companies
        .Where(c => !c.Coverage_status.Contains("drop"))
        .Select(n => new
        {
            FSTick = string.Format("{0}-{1}", n.Ticker,
                csvValues
                    .Where(v => v.Exchange.Contains(n.Exchange))
                    .Select(v => v.Lookup).FirstOrDefault())
        });

    foreach (var r in result)
        Console.WriteLine(r.FSTick);

郑重声明,这段代码绝对不是性能方面的。


输出: