2017-08-09 87 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)