linq 查询:列表作为输入参数
假设我们有 2 个对象模型:
class MyModel1{
int ModelID {get;set;}
string Var2 {get;set;}
}
class MyModel2{
int ModelID {get;set;}
string Var2 {get;set;}
}
假设我们有一个 linq to sql 查询,它返回 MyModel1 的列表,如下所示:
var OutputModel1 = from t in MyDataContext
where...
select new MyModel1
{...}.ToList();
Return OutputModel1 as List<MyModel1>;
现在我有了这个列表,我想将此列表传递给另一个查询,该查询将返回ModelID 相同的 MyModel2 的列表。
如果您对如何执行此操作有任何建议,请告诉我。
谢谢。
Let's say we have 2 object models:
class MyModel1{
int ModelID {get;set;}
string Var2 {get;set;}
}
class MyModel2{
int ModelID {get;set;}
string Var2 {get;set;}
}
Let's say that we have a linq to sql query that returns a list of MyModel1 like this:
var OutputModel1 = from t in MyDataContext
where...
select new MyModel1
{...}.ToList();
Return OutputModel1 as List<MyModel1>;
Now that I have this list, I'd like to pass this list to another query that'll return a list of of MyModel2 where the ModelID is the same.
Let me know if you have some suggestions on how to do this.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您只是想稍后为每个
MyModel1
创建一个MyModel2
副本,那么很简单:如果您需要查询具有相同 id 的那些 MyModel2 对象的数据上下文作为 MyModel1 对象(即存在关系),那么它是不同的(但您应该提供该信息)。
PS:不需要写成List——
ToList
的返回值就是一个List
,不再需要了,不少于。If you simply want to make a
MyModel2
copy of eachMyModel1
some point afterwards, then it's simple:If you need to query the data context for those MyModel2 objects with the same ids as the MyModel1 objects (i.e., there is a relation), then it's different (but you should provide that information).
PS: There's no need to write
as List<MyModel1>
-- the return value ofToList
is exactly aList<MyModel1>
, no more, no less.假设您正在寻找现有 Model2 的子集,您可以使用如下所示的内容:
“join”函数类似于 SQL 中的内部联接,并且不会返回任何不匹配的 t2。
有关于连接和其他内容的相当详细的描述:
http://msdn.microsoft.com/en-us/library/bb397941 .aspx#Y275
Assuming that you're looking for a subset of existing Model2's, you could use something like the following:
The "join" functions like an inner join in SQL, and won't return any t2's that don't have a match.
There's a fairly detailed description of joins and whatnot at:
http://msdn.microsoft.com/en-us/library/bb397941.aspx#Y275