2017-08-09 4 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 
     } 
    } 
} 

UPDATE私は私の質問にいくつかの変更を行いました。そこ

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

、あなたはどのHistoryPricehList.Containsがあるかどうかをチェックしている:

+0

あなたの 'Get'メソッドは何をしますか?あなたのコードの残りの部分は、あなたが望むものに合わないものは何ですか? – krillgar

+0

@krillgarこれは、そのシンボルのHistoricalDataアイテムの完全なリストを返すだけです。データベースに存在しないすべてのHistoricalDataアイテムをデータベース内に追加しようとしています。 – user3610374

+0

Didあなたはこれを試して?それは動作しませんか? – krillgar

答えて

1

は、あなたが持っている擬似コードによると、これは私はあなたがコメントで言及エラーを見ているところであります)オブジェクトは、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) 
関連する問題