关于linq的用法

最近在用linq那家伙,爽哇.基本不用自己写啥代码,拿来就可以用哇,

查询方法如:

代码
基本语法
var result 
= from item 
                  
in container 
                  orderby value ascending
/descending 
                  select item; 
1、获取全部记录
var allCars 
= from c in myCars select c; 
2、只获取字段名称
var names 
= from c in myCars select c.PetName; 这里names就是隐式类型的变量。
3、使用Enumerable.Distinct<T>()
var makes 
= (from c in myCars select c.Make).Distinct < string > (); 
4、即可以在定义的时候调用Enumberalbe扩展函数
var names 
= from c in myCars select c.PetName;
foreach (var n in names)
{
Console.WriteLine( 
" Name: {0} " , n);

也可以在兼容的数组类型上调用
var makes 
= from c in myCars select c.Make;
Console.WriteLine( 
" Distinct makes: " );
foreach (var m in makes.Distinct < string > ())
{
Console.WriteLine( 
" Make: {0} " , m);

// Now get only the BMWs. 
var onlyBMWs = from c in myCars where c.Make == " BMW " select c; 
// Get BMWs going at least 100 mph. 
var onlyFastBMWs = from c in myCars
where c.Make == " BMW " && c.Speed >= 100 
select c;  

5、生成新的数据类型(投影)


var makesColors 
= from c in myCars select new {c.Make, c.Color}; 

6、Reverse<T>()


var subset 
= (from c in myCars select c).Reverse < Car > ();
foreach (Car c in subset)
{
Console.WriteLine(
"{0} is going {1} MPH", c.PetName, c.Speed);


或者


var subset 
= from c in myCars select c;
foreach (Car c in subset.Reverse < Car > ())
{
Console.WriteLine(c.ToString());


7、排序
默认是ascending


// Order all the cars by PetName. 
var subset = from c in myCars orderby c.PetName select c; // Now find the cars that are going less than 55 mph,
// and order by descending PetName 
subset = from c in myCars
where c.Speed > 55 orderby c.PetName descending select c; 

默认顺序时也可以明确指明


var subset 
= from c in myCars
orderby c.PetName ascending select c; 

8、Enumerable.Except()
两个IEnumerable
<T>兼容的对象的差集



static void GetDiff()
{
List 
< string > myCars = new List < String > 
" Yugo " , " Aztec " , " BMW " };
List 
< string > yourCars = new List < String > 
" BMW " , " Saab " , " Aztec " };
var carDiff 
= (from c in myCars select c)
.Except(from c2 
in yourCars select c2);
Console.WriteLine( 
" Here is what you don't have, but I do: " );
foreach ( string s in carDiff)
Console.WriteLine(s); 
// Prints Yugo. 


代码
使用LINQ查询结果

如果查询结果是强类型的,如 
string[],List<T>等,就可以不用var类型,而是使用合适的IEnumerable<T>或 IEnumerable(因为IEnumerable<T>也扩展了IEnumerable)类型。


class Program
{
static void Main( string [] args)
{
Console.WriteLine( 
" ***** LINQ Transformations *****"" );
IEnumerable < string > subset = GetStringSubset();
foreach ( string item in subset)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
static IEnumerable < string > GetStringSubset()
{
string [] currentVideoGames = { " Morrowind " , " BioShock " ,
" Half Life 2: Episode 1 " , " The Darkness " ,
" Daxter " , " System Shock 2 " };
// Note subset is an IEnumerable<string> compatible object. 
IEnumerable < string > subset = from g in currentVideoGames
where g.Length > 6 
orderby g
select g;
return subset;
}


昨天晚上在家里整一个update 功能.结果就没找着..因为家里当时不能上网,有点晕.找了很久也没找到办法,

原来呢是这么简单实现的:

 public bool Update(CardType model)
        {
           
               
var single = dc.CardType.Single(record => record.ID == model.ID);
                single.CardTypeName
= model.CardTypeName;
                single.Remark
= model.Remark;
                dc.SubmitChanges();
        }

-------长沙程序员技术交流QQ群:428755207-------
原文地址:https://www.cnblogs.com/qq4004229/p/1812304.html