+ 4
what is Linq and how to use Linq in c#?
3 Respuestas
+ 4
What have you already tried after reading that chapter in the course?
If you post your code, I'll be more than happy to take a look at it and figure out the errors with you.
+ 4
Linq stands for Language Integrated Query, and if you've done the SoloLearn course for SQL, you'd know all about queries. Here's a quick and simple example of Linq, where where construct a query to collect all even numbers in an array.
int[] arr = new int[6] { 1, 2, 3, 4, 5, 6 };
var evens = from x in arr where x % 2 == 0 select x;
Console.WriteLine(string.Join(", ", evens));
+ 3
Language-Integrated Query,LINQ is a structured query syntax built in C# and VB.NET used to save and retrieve data from different types of data sources like an Object Collection, SQL server database, XML, web service etc.
class Program
{
static void Main(string[] args)
{
Student[] studentArray = {
new Student() { StudentID = 1, StudentName = "Siobhan", age = 23 } ,
new Student() { StudentID = 2, StudentName = "Aoife", age = 21 } ,
new Student() { StudentID = 3, StudentName = "Caoimhe", age = 25 } ,
new Student() { StudentID = 4, StudentName = "Niamh" , age = 20 } ,
new Student() { StudentID = 5, StudentName = "Aine" , age = 31 } ,
new Student() { StudentID = 6, StudentName = "Blathnaid", age = 17 } ,
new Student() { StudentID = 7, StudentName = "Muirne",age = 19 } ,
};
// Use LINQ to find teenager students
Student[] teenAgerStudents = studentArray.Where(s => s.age > 12 && s.age
< 20).ToArray();
// Use LINQ to find first student whose name is Siobhan
Student Siobhan = studentArray.Where(s => s.StudentName ==
"Siobhan").FirstOrDefault();
// Use LINQ to find student whose StudentID is 5
Student student5 = studentArray.Where(s => s.StudentID == 5).FirstOrDefault();
}
}