Linq

The types of LINQ are mentioned below in brief.
 LINQ to Objects
 LINQ to XML(XLINQ)
 LINQ to DataSet
 LINQ to SQL (DLINQ)
 LINQ to Entities
Apart from the above, there is also a LINQ type named PLINQ which is Microsoft’s parallel LINQ.

 

 

var list = (from e in employees
join d in departments on e.DepartmentId equals d.DepartmentId
select new
{
EmployeeName = e.EmployeeName,
DepartmentName = d.Name,
});

foreach(var e in list)
{
Console.WriteLine("Employee name={0},Department Name={1}",e.EmployeeName,e.DepartmentName);
}

 

 

 

List<string> phrases = new List<string> { "an apple a day", "the quick brown fox" };

var query = from phrase in phrases
from word in phrase.Split(' ')
select word;

foreach(var s in query)
{
Console.WriteLine(s);
}

Console.ReadLine();

 

 

static void Main(string[] args)
{
int[] num = { -20, 12, 6, 10, 0, -3, 1 };

var posNum = from n in num
orderby n
select n;

Console.WriteLine("Values in ascending order: ");

foreach(int i in posNum)
{
Console.WriteLine(i);
}

var posNumsDesc = from n in num
orderby n descending
select n;

Console.WriteLine("\nValues in descending order: ");

foreach(int i in posNumsDesc)
{
Console.WriteLine(i);
}

Console.ReadLine();
}

 

 

static void Main(string[] args)
{
List<int> numbers = new List<int>() { 35, 44, 200, 84, 3987, 4, 199, 329, 446, 208 };

IEnumerable<IGrouping<int, int>> query = from num in numbers
group num by num % 2;

foreach(var group in query)
{
Console.WriteLine(group.Key == 0 ? "\nEven numbers: " : "\nOdd numbers: ");

foreach(var i in group)
{
Console.WriteLine(i);
}
}

Console.ReadLine();

}

 

class Program
{
static void Main(string[] args)
{
Plant[] plants = new Plant[]
{
new CarnivorousPlant
{
Name="Venus Fly Trap",
TrapType="Snap Trap"
},
new CarnivorousPlant
{
Name="Pitcher Plant",
TrapType="Pitfall Trap"
},

new CarnivorousPlant
{
Name="Sundew",
TrapType="Flaypaper Trap"
},

new CarnivorousPlant
{
Name="WaterWheel Plant",
TrapType="Snap Trap"
}
};

var query = from CarnivorousPlant cPlant in plants
where cPlant.TrapType == "Snap Trap"
select cPlant;

foreach(var q in query)
{
Console.WriteLine("Name={0},Trap Type={1}", q.Name, q.TrapType);
}

Console.WriteLine("\nPress any key to continue");
Console.ReadLine();
}
}

class Plant
{
public string Name { get; set; }
}

class CarnivorousPlant:Plant
{
public string TrapType { get; set; }
}

 

class Program
{
static void Main(string[] args)
{
Pet[] cats = GetCats();
Pet[] dogs = GetDogs();

IEnumerable<string> query = cats.Select(cat => cat.Name).Concat(dogs.Select(dog => dog.Name));
foreach(var e in query)
{
Console.WriteLine("Name={0}", e);
}

Console.WriteLine("\nPress any key to continue");
Console.ReadLine();
}

static Pet[] GetCats()
{
Pet[] cats = {new Pet { Name="Barley",Age=8},
new Pet{Name="Boots",Age=4},
new Pet{Name="Whiskers",Age=1}};
return cats;
}

static Pet[] GetDogs()
{
Pet[] dogs =
{
new Pet{Name="Bounder",Age=3},
new Pet{Name="Snoopy",Age=14},
new Pet{Name="Fido",Age=9}
};

return dogs;
}
}

class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}

原文链接: https://www.cnblogs.com/Fred1987/p/6363857.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    Linq

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/402259

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月19日 上午9:40
下一篇 2023年4月19日 上午9:40

相关推荐