C# 如何修复此linq语句?
我正在将实体框架与C#一起使用,我不确定如何处理这段代码。下面是我的伪代码,我有一个股票交易列表,我只想插入数据库表中不存在的交易,但我不确定如何编写linq代码来完成这项工作 此当前代码导致以下错误:C# 如何修复此linq语句?,c#,linq,C#,Linq,我正在将实体框架与C#一起使用,我不确定如何处理这段代码。下面是我的伪代码,我有一个股票交易列表,我只想插入数据库表中不存在的交易,但我不确定如何编写linq代码来完成这项工作 此当前代码导致以下错误: Argument 1: cannot convert from 'System.Collections.Generic.IEnumerable<System.DateTime>' to 'Finance.Program.HistoryPrice' public class Dail
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
}
}
}
参数1:无法从“System.Collections.Generic.IEnumerable”转换为“Finance.Program.HistoryPrice”
公共类每日扩展数据
{
公共int ID{get;set;}
公共字符串符号{get;set;}
公共日期时间日期{get;set;}
公共十进制开放{get;set;}
公共十进制高位{get;set;}
公共十进制低位{get;set;}
公共十进制关闭{get;set;}
公共十进制调整关闭{get;set;}
公共int卷{get;set;}
}
公共类历史价格
{
公共日期时间日期{get;set;}
公共十进制开放{get;set;}
公共十进制高位{get;set;}
公共十进制低位{get;set;}
公共十进制关闭{get;set;}
公共Int32卷{get;set;}
公共十进制AdjClose{get;set;}
}
使用(financeEntities上下文=新的financeEntities())
{
foreach(符号中的字符串符号)
{
List hList=Get(符号,新日期时间(1900,1,1),DateTime.UtcNow);
List aList=wait(context.dailyamexdata.Where(c=>c.Symbol==Symbol&&hList.Contains(hList.Select(i=>i.Date))==false.toListSync());//我编写的伪代码,希望您能理解我在这里要做的事情,即只返回数据库中尚不存在的DailyAmexData列表,然后将这些项保存到数据库中
foreach(列表中的DailyAmexData项)
{
//在数据库表中插入项的值
}
}
}
更新我对我的问题做了一些修改,这样你就可以理解我想做什么 根据您的伪代码,这就是您在评论中提到的错误:
hList.Contains(hList.Select(i => i.Date))
在这里,您正在检查是否有任何HistoryPrice
(hList.Contains
)对象等于IEnumerable
(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)
你的
Get
方法做什么?您的代码的其余部分做了什么不符合您的要求?@krillgar它只是返回该符号的完整历史数据项列表,我正在尝试查找该列表中数据库中不存在的所有历史数据项,以便我只能将新项添加到数据库中。您尝试了吗?它不起作用吗?我想,您正在查询中寻找类似的内容-为了子孙后代,请在您的问题中包含该错误消息