linq 查询:列表作为输入参数

发布于 2024-10-29 05:35:10 字数 546 浏览 1 评论 0原文

假设我们有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一瞬间的火花 2024-11-05 05:35:10

如果您只是想稍后为每个 MyModel1 创建一个 MyModel2 副本,那么很简单:

var OutputModel2 = OutputModel1.Select(m =>
    new MyModel2 { 
        ModelId = m.ModelId,
        Var2 = m.Var2,
    }).ToList();

如果您需要查询具有相同 id 的那些 MyModel2 对象的数据上下文作为 MyModel1 对象(即存在关系),那么它是不同的(但您应该提供该信息)。

PS:不需要写成List——ToList的返回值就是一个List,不再需要了,不少于。

If you simply want to make a MyModel2 copy of each MyModel1 some point afterwards, then it's simple:

var OutputModel2 = OutputModel1.Select(m =>
    new MyModel2 { 
        ModelId = m.ModelId,
        Var2 = m.Var2,
    }).ToList();

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 of ToList is exactly a List<MyModel1>, no more, no less.

掐死时间 2024-11-05 05:35:10
var OutputModel2 = (from m in MyDataContext.Model2Collection where MyDataContext.Model1Collection.Any(x => x.ModelID == m.ModelID && x.Var2 == 3/*PUT YOUR WHERE CLAUSE HERE*/)).ToList();
var OutputModel2 = (from m in MyDataContext.Model2Collection where MyDataContext.Model1Collection.Any(x => x.ModelID == m.ModelID && x.Var2 == 3/*PUT YOUR WHERE CLAUSE HERE*/)).ToList();
你是暖光i 2024-11-05 05:35:10

假设您正在寻找现有 Model2 的子集,您可以使用如下所示的内容:

var OutputModel2 = from t2 in CollectionOfModel2s
                           join t1 in OutputModel1 on t2.ModelID equals t1.ModelID
                           select t2;

“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:

var OutputModel2 = from t2 in CollectionOfModel2s
                           join t1 in OutputModel1 on t2.ModelID equals t1.ModelID
                           select t2;

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文