在linq-to-sql中使用.Contains() [英] using .Contains() with linq-to-sql

查看:65
本文介绍了在linq-to-sql中使用.Contains()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下查询,它接收一个整数列表作为参数:

I have the following query that receives a list of ints as a parameter:

public int GetMostRecent(List<int> TheIDs)
{
 ...using MyDC...

   var TheMostRecentID = (from d in MyDC.Data
                           where TheIDs.Contains(d.ID)
                           orderby d.DateTime
                           select d.ID).LastOrDefault(); 
}

这是将参数集合中的列表与数据库中的数据匹配的最佳方法,还是比在linq-to-sql中使用.Contains()方法更好的方法.

Is this the best way to match a list within a parameter collection to data in the database or is there a better way than using the .Contains() method in linq-to-sql.

谢谢.

推荐答案

您所拥有的是正确的.它将使用集合中提供的值转换为SQL中的 IN 子句.

What you have is correct. This will be translated into an IN clause in SQL with the values supplied in the collection.

不相关的注释是,您应该尝试按日期降序对查询进行排序,并使用 FirstOrDefault().现在,您将带回整个结果集,并丢弃除一行以外的每一行.

On an unrelated note, you should try ordering the query by date descending and use FirstOrDefault(). As it is now, you're going to bring back the entire result set and throw away every row but one.

这篇关于在linq-to-sql中使用.Contains()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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