-5

I'm using entity framework with C# and I'm unsure how to proceed with this code. My pseudo code is below and I have a list of stock transactions and I'm only trying to insert the transactions that don't exist in the database table but I'm not sure how to write the linq code to do this.

This current code gives me this error:

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 I made some changes to my question so you can understand what I'm trying to do.

DarthVegan
  • 1,501
  • 4
  • 23
  • 34
  • What does your `Get` method do? What does the rest of your code do that doesn't fit what you want? – krillgar Aug 09 '17 at 18:39
  • @krillgar It just returns the full list of HistoricalData items for that symbol and I'm trying to find all HistoricalData items in that list that don't exist in the database yet so I can add the new items only to the database – DarthVegan Aug 09 '17 at 18:45
  • Did you try this? Does it not work? – krillgar Aug 09 '17 at 18:47
  • @krillgar No this was only pseudo code to illustrate what I'm trying to do. This current code gives me this error Argument 1: cannot convert from 'System.Collections.Generic.IEnumerable' to 'Finance.Program.HistoryPrice' – DarthVegan Aug 09 '17 at 18:50
  • @user3610374 that error will be use to `hList.Contains(hList.Select(i => i.Date))`. to me this bit makes no sense, what are you trying to do here? – Matt Aug 09 '17 at 18:53
  • @Matt I'm new o C# and EF but what I'm trying to do is return only the items in the hList that don't exist in the database yet. I tried to do a linq search for items that have the same symbol and to see if the date for that item exists in the database already – DarthVegan Aug 09 '17 at 18:56
  • 1
    I assume, you are looking for something like this in your query- [https://stackoverflow.com/questions/879391/linq-any-vs-exists-whats-the-difference](https://stackoverflow.com/questions/879391/linq-any-vs-exists-whats-the-difference) – Pankajyt Aug 09 '17 at 18:59
  • 1
    For posterity sake, please include that error message in your question – krillgar Aug 09 '17 at 19:11
  • @krillgar I added that error message to my question – DarthVegan Aug 09 '17 at 19:16

1 Answers1

1

According to the pseudocode that you have, this is where I'm seeing the error that you mention in the comments:

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

There, you're checking to see if there is any HistoryPrice (hList.Contains) object that is equal to an IEnumerable<DateTime> (hList.Select).

Also, you're not utilizing the DailyAmexData in your main LINQ query within the database. Change that piece of your LINQ to the following:

// 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)
krillgar
  • 11,554
  • 6
  • 43
  • 81