Home > front end >  Linq statement
Linq statement

Time:12-03

Basic syntax
Var result=the from item
In the container
Orderby value ascending/item first-just
Select the item.

1. Access to all records in var allCars=the from c myCars select c;

Extract (specify rows)
Var allCars=(from c myCars in the select c) Take (5);//before extracting five data

(fuzzy query)
Var quest=the from qu in MDC. Question
Where qu. Question1. The Contains (" number ")
The select qu;//fuzzy query
-- -- -- -- -- -- -- -- qu. Question1. The Contains (" number ")//Linq which column of the table of the Contains () method of

(Linq by adding the data object to the database)
NewsInfo newnf=new NewsInfo ();
Newnf. Newscontext=nf. Newscontext;
Newnf.=newsauthor nf. Newsauthor;
Newnf. Newstime=DateTime. Now;//get the system time
The MDC. NewsInfo. InsertOnSubmit (newnf);//tag to add status
The MDC. SubmitChanges ();//perform add
2. The only access to the field names in var names=the from c myCars select c.P etName; Names here is implicitly typed variables,

3. Use the Enumerable. Distinct (var) top service=(from c in myCars select c. ake). Distinct (a);

4. That can be called when defined Enumberalbe extension function var names=c in the from myCars select c.P etName;
The foreach n in names (var)
{the Console. WriteLine (" Name: {0} ", n); }


Can also call on compatible an array type
Var top service=c in the from myCars select c. ake.
Console. WriteLine (" Distinct top service: ");
Foreach (var m in top service Distinct ())
{
Console. WriteLine (" the Make: {0} ", m);
}
Var onlyBMWs=c in the from myCars
Where c. ake=="BMW" select c;

Var onlyFastBMWs=c in the from myCars
Where c. ake=="BMW" & amp; & C.s. peed & gt;=100 select c;

5. Generate new data types (projection) var makesColors=c in the from myCars select new {c. ake, Arthur c. olor}; 6. Reverse (var) subset=(from c myCars in the select c) Reverse (a);
The foreach (Car c subset in)
{
Console. WriteLine (" {0} is going {1} "MPH, c.P etName, c.s. peed);
}
Or
Var subset=the from c myCars in the select c;
Foreach (Car c subset in. Reverse ())
{
Console. WriteLine (c.T oString ());
}
7. Default is ascending sort
Var subset=c in the from myCars orderby c.P etName select c;
Subset in myCars=the from c
Where c.s. peed & gt; 55 orderby c.P etName item first-just select c;

Can also be made clear when the default order
Var subset=c in the from myCars
Orderby c.P etName ascending select c;
8. Enumerable. Except () two IEnumerable Compatible with the object of difference set

The static void GetDiff ()
{
List MyCars=new List {" Yugo ", "along", "BMW"};
List YourCars=new List {" BMW ", "Saab", "Aztec"};
Var carDiff=(from c myCars in the select c.) the Except (from c2 in yourCars
Select the c2);
Console. WriteLine (" Here is what you don 't have, but I do: ");
The foreach (string s in carDiff) Console. WriteLine (s);
}

///& lt; Summary>
///add a pick to press
///& lt;/summary>
///& lt; Param name="news" & gt;
Public void AddNews (News News)
{
GameLinq. News. InsertOnSubmit (News);
GameLinq. SubmitChanges ();
}

///& lt; Summary>
///delete one day news by ID
///& lt;/summary>
///& lt; Param name="id" & gt;
Public void DeleteNews (int id)
{
Var news=the from p in gameLinq. News
Where p.N ewsId==id select p;
GameLinq. News. DeleteAllOnSubmit (News);
GameLinq. SubmitChanges ();
}
///& lt; Summary>
///day update news
///& lt;/summary>
///& lt; Param name="n" & gt;
Public void UpdateNews (News n)
{
Var news=the from p in gameLinq. News
Where p.N ewsId==n.N ewsId select p;
News va=News. The First ();
Va. The Content=n.C ontent;
Va. NewsState=n.N ewsState;
Va.=NewsTime n.N ewsTime;
Va. Title=n.T itle;
GameLinq. SubmitChanges ();
}
///& lt; Summary>
///get an object
///& lt;/summary>
Public the UserInfo GetUserByUserid (string userid)
{
Var u=the from p in MyOfficeLinq. The UserInfo
Where p.U serId==userid select p;
The UserInfo user=u.F irst ();
return user;
}
///& lt; Summary>
///gets so object collection
///& lt;/summary>
Public List GetAllRoleRight ()
{
List List=new List (a);
Var r=the from p in MyOfficeLinq. RoleRight select p;
Foreach (RoleRight item in r)
{
List. The Add (item);
}
return list;
}

CodePudding user response:

The original poster and this is wrote about? Is question is to share? Calculating do not calculate water stick

CodePudding user response:

C # the lambda writing significance as if is not big, because the SQL statement in the actual application complexity may be higher,
  • Related