[英]linq query on list id's
I have a list of id's and I need to get all records correscponding to each id in the list . 我有一个ID列表,我需要获取与列表中每个ID对应的所有记录。
below is the sample code 下面是示例代码
public List<Items> LoadLineItemByPkeys(IEnumerable<long> ItemId)
{
var ItemList = CurrentSession.Query<LineItem>()
.Where(l =>itemId.Contains(l.id))
.ToList();
return ItemList ;
}
but i am getting exception 但我越来越例外
An exception of type 'NHibernate.Exceptions.GenericADOException' occurred in NHibernate.dll but was not handled in user code NHibernate.dll中发生类型为'NHibernate.Exceptions.GenericADOException'的异常,但未在用户代码中处理
inner exception:- 内部异常:
Failed to convert parameter value from a WhereSelectArrayIterator`2 to a Int64. 无法将参数值从WhereSelectArrayIterator`2转换为Int64。 Please help me on this. 请帮我。
when i executed same query in LINQPad, i am getting the results. 当我在LINQPad中执行相同的查询时,我得到的结果。
You may be having trouble due to how you've passed in the IDs. 您可能由于传递ID的方式而遇到麻烦。 You may find that converting that sequence into a list or an array would help: 您可能会发现将该序列转换为列表或数组会有所帮助:
// Various names to be more conventional/readable
public List<Items> LoadLineItemsById(IEnumerable<long> ids)
{
var idList = ids.ToList();
return CurrentSession.Query<LineItem>()
.Where(item => idList.Contains(item.Id))
.ToList();
}
If i'm not mistaking you cannot make use of the contains method. 如果我没有误会,您将无法使用contains方法。
You'll have to make use of .IsIn() 您必须使用.IsIn()
public List<Items> LoadLineItemByPkeys(IEnumerable<long> itemIds)
{
var lineItemList = CurrentSession.QueryOver<LineItem>()
.WhereRestrictionOn(l =>l.id).IsIn(itemIds).List()
.ToList();
return lineItemList ;
}
This wil generate a sql statement with an in operator. 这将生成带有in运算符的sql语句。 Be aware of the limitations of this operator. 请注意此运算符的局限性。 Some db's have a limit of 1000 arguments, other of 2000 etc. 某些数据库的参数限制为1000,其他参数的限制为2000等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.