2017-08-09 83 views
-5

我正在使用C#实体框架,我不确定如何继续使用此代码。我的伪代码如下,我有一个股票交易清单,我只是试图插入数据库表中不存在的交易,但我不知道如何编写linq代码来做到这一点。如何解决这个linq语句?

这个当前的代码给了我这个错误:

Argument 1: cannot convert from 'System.Collections.Generic.IEnumerable<System.DateTime>' to 'Finance.Program.HistoryPrice' 

public class DailyAmexData 
{ 
    public int ID { get; set; } 
    public string Symbol { get; set; } 
    public DateTime Date { get; set; } 
    public decimal Open { get; set; } 
    public decimal High { get; set; } 
    public decimal Low { get; set; } 
    public decimal Close { get; set; } 
    public decimal AdjustedClose { get; set; } 
    public int Volume { get; set; } 
} 

public class HistoryPrice 
{ 
    public DateTime Date { get; set; } 
    public decimal Open { get; set; } 
    public decimal High { get; set; } 
    public decimal Low { get; set; } 
    public decimal Close { get; set; } 
    public Int32 Volume { get; set; } 
    public decimal AdjClose { get; set; } 
} 

using (financeEntities context = new financeEntities()) 
{ 
    foreach (string symbol in symbols) 
    { 
     List<HistoryPrice> hList = Get(symbol, new DateTime(1900, 1, 1), DateTime.UtcNow); 
     List<DailyAmexData> aList = await (context.DailyAmexDatas.Where(c => c.Symbol == symbol && hList.Contains(hList.Select(i => i.Date)) == false).ToListAsync<DailyAmexData>()); // pseudo code that I wrote so you hopefully understand what I'm trying to do here which is only return a list of DailyAmexData that doesn't exist in the database yet and then save those items to the database 

     foreach(DailyAmexData item in aList) 
     { 
      // insert values of item in database table 
     } 
    } 
} 

更新我做了一些改变,以我的问题,所以你能理解我想要做的事。

+0

你的'Get'方法是做什么的?你的其他代码做了什么不符合你的要求? – krillgar

+0

@krillgar它只是返回该符号的HistoricalData项目的完整列表,我试图找到该列表中所有不存在于数据库中的HistoricalData项目,因此我只能将新项目添加到数据库 – user3610374

+0

Did你试试这个吗?它不起作用吗? – krillgar

回答

1

根据你有伪代码,这就是我看到你在评论中提到的错误:

hList.Contains(hList.Select(i => i.Date)) 

在那里,你检查,看看是否有任何HistoryPricehList.Contains )对象等于IEnumerable<DateTime>hList.Select)。

另外,您在数据库中的主LINQ查询中没有使用DailyAmexData。将你的LINQ改为如下:

// Before the 2nd LINQ statement 
var priceDates = hList.Select(hp => hp.Date).ToList(); 

// Then inside your LINQ statement, replacing the piece I pointed out before. 
priceDates.Contains(c.Date)