如何使用C#中的列表执行查询 [英] How do I perform a query with a list in C#

查看:105
本文介绍了如何使用C#中的列表执行查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道我是否可以像这样执行查询:



我尝试了什么:



Hi, I just was wondering if I can perform a Query just like that:

What I have tried:

List <studentclasslist> = _context.StudentClass
                    .Where(sc => sc.ClassId == 1)
                    .ToList();
 
List<int> studentIdList = new List<int>(); 

foreach (var student in studentClassList )
{
   studentIdList = _context.Student.Where(st => st.Id == student.StudentId).ToList(); 
}





提前致谢。



Thanks in advance.

推荐答案

To我这种代码......显然不会编译...表明你还没有真正研究过使用Linq提供的强大工具的基础知识。



Don不过不用担心:我认识的每个人都经历了学习曲线,以便能够流利地使用Linq :)好吧,好吧,imho,在数据库查询语言方面有深厚背景的人有更轻松的时间;我只是不认识他们:)



请考虑购买一本关于Linq的书,并使用在线工具LinqPad [ ^ ],由Albihari兄弟提供。他们在Linq上有几本书;并且,您可能会觉得这很有用:[ ^ ]。



我找到了我的学生对'Where运算符是'选择函数的想法反应良好;并且,'Select运算符是一个枚举元素并将其转换为另一种形式的函数。
To me this kind of code ... that will obviously not compile ... indicates you have not really studied the basics of using the powerful tools that Linq provides.

Don't worry, though: everyone I know has gone through a learning curve to get somewhat fluent in using Linq :) Well, okay, imho, people with a deep background in DB query languages have an easier time; I just don't know them :)

Please consider getting a book on Linq, and using the on-line tool, LinqPad [^], provided by the Albihari brothers. They have several books out on Linq; and, you may find this useful: [^].

I have found my students respond well to the idea that the 'Where operator is a "selection function;" and, the 'Select operator is a function that enumerates and transforms elements into another form.


只需添加,您需要包含Linq命名空间以便开始工作,否则你'将有一个List类但不是那些运算符



Just to add, you need to include the Linq namespace for that stuff to start working, otherwise you'll have a List class but not those operators

List<int> studentIdList = new List<int>(); 





此外,这没有任何意义。如果需要在范围内设置它,请将其设置为null。你的代码一遍又一遍地设置它并且不做任何事情吗?实际上,您的代码将无法编译。当你问一个问题时发布实际代码....



Also, this makes no sense. Set it to null if you need to set it inside a scope. Your code sets it over and over and does nothing with it? In fact, your code won't compile. Post real code when you ask a question....


嗯,你可以......如果你的名字是正确的,C#是区分大小写的...但它不会做很有用。



循环内的赋值意味着最终结果只是最终结果,因为它每次都抛弃所有先前的结果循环。



你可能想要考虑List.AddRange而不是赋值,但是你猜想你可能想要考虑一下你的数据以及它们之间的关系。而不是跳到这样的代码 - 即使不知道你想要做什么,这看起来是一种效率很低的方式!
Well, you could ... if you got the names right, C# is case sensitive ... but it wouldn't do much that was useful.

The assignment inside the loop means that the eventual result will be the final result only, because it "throws away" all previous results each time round the loop.

You might want to consider List.AddRange instead of an assignment, but at a guess you might want to think about your data and the relationships between them rather than leaping into code like that - even without knowing exactly what you are trying to do that looks like a very inefficient way to do things!


这篇关于如何使用C#中的列表执行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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