LINQ-TO-SQL查询不使用CSV文件

时间:2016-05-30 14:37:58

标签: c# linq linq-to-sql

我正在从我的程序中读取一个简单的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查询来实现我想要的结果?

2 个答案:

答案 0 :(得分:2)

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

    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())
    };

答案 1 :(得分:0)

根据我上面的评论,如果将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);

为了记录,这段代码肯定是在性能方面。

输出:

enter image description here