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; }
}

posted @   FredGrit  阅读(161)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示