Linq To Entities将值与List进行比较< int> [英] Linq To Entities Compare Value Against List<int>

查看:70
本文介绍了Linq To Entities将值与List进行比较< int>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Entity Framework 5.0 ,并且LINQ查询有问题.我有以下方法,该方法接受一个整数值,然后将其传递到查询中.效果很好.

I am using Entity Framework 5.0, and I have a problem with a LINQ query. I have the following method that accepts an integer value which is then passed into the query. This works fine.

public IList<tblcoursebooking> GetStandardReport(int AttendanceID)
{
    return _UoW.tblcoursebookingRepo.All
           .Where(cb => cb.Attended.Equals(AttendanceID)
           .ToList();

} 

但是,我需要更改方法,以使其接受整数列表,然后提取所有 Attended 等于任何List of的记录.整数.像这样

However, I need to change the method so that it accepts a List of integers, and then pulls out all records where Attended is equal to any of the List of integers. Something like this

public IList<tblcoursebooking> GetStandardReport(List<int> AttendanceIDs)
{
    return _UoW.tblcoursebookingRepo.All
           .Where(cb => cb.Attended.Equals == any AttendanceIDs
           .ToList();
} 

我想尝试使用包含任何 LINQ关键字,但是,参加是单个值,而不是集合,点后唯一可用的属性是

I would like to do try and use the Contains or Any LINQ keywords, however, as Attended is a single value, not a collection, the only properties available to me after the dot are

CompareTo, 等于, GetHashCode, GetType, GetTypeCode, ToString

CompareTo, Equals, GetHashCode, GetType, GetTypeCode, ToString

有人可以帮忙吗?

感谢您的时间.

推荐答案

使用Contains函数,它将使每个ID与给定列表匹配:

Use the Contains function, it will match each ID against the given list:

return _UoW.tblcoursebookingRepo.All
       .Where(cb => AttendanceIDs.Contains(cb.Attended))
       .ToList();

通常,请记住,Where子句不过是带有嵌套if语句(虽然非常漂亮)的feach foreach.它需要一个计算结果为布尔值的表达式.如果您只需要检查一项而不使用LinQ,就会很快想到:

In general, just keep in mind that a Where clause is nothing more than a fancy foreach with a nested if-statement (a very fancy one, though). It needs an expression that evaluates to a boolean. If you only had one item to check, without using LinQ, you'd quickly come up with something like:

if(AttendanceIDs.Contains(myItem.Attended))

您可以使用完全相同的方式来处理LinQ的Where子句,如上所示:)如果您感到困惑,只需考虑一下如何进行一次检查,因为LinQ会为您完成迭代部分.

You can treat LinQ's Where clauses in exactly the same way, as shown above :) If you're stumped, just think of how you would check it a single time, because LinQ will do the iterative part for you.

更新

如Faisal的回答所述,WhereIn提供了类似的功能.尚未使用过,但似乎是一种更简洁的方法.

As mentioned in Faisal's answer, WhereIn provides a similar functionality. Haven't used it yet, but it seems a more concise approach.

我并没有改变答案,因为我认为更重要的是指出如何在Where子句中使用布尔值评估,这也将有助于解决以后可能遇到的所有类似问题. WhereIn将不相关.

I'm not changing my answer though, as I feel it is more important to point out how you can use the boolean evaluation in a Where clause, this should also help for all future similar issues you might encounter where a WhereIn will not be relevant.

但是,在这种特殊情况下,您也可以使用WhereIn:-)

But nonetheless, you could as well use WhereIn in this particular case :-)

这篇关于Linq To Entities将值与List进行比较&lt; int&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆