In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you the "LINQ to SQL Select how to achieve simple form, anonymous type form, conditional form", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "LINQ to SQL Select how to achieve simple form, anonymous type form, conditional form" this article.
1. Simple form:
Var Q = from c in db.Customers select c.ContactName
Note: this statement is only a declaration or a description, it does not really take the data out, it will only execute this statement when you need the data, which is called deferred loading (deferred loading). If the result set returned at the time of declaration is a collection of objects. You can use the ToList () or ToArray () methods to save the query results before querying the collection. Of course, lazy loading (deferred loading) can concatenate the query syntax like concatenating SQL statements, and then execute it.
two。 Anonymous type form:
Note: anonymous typing is a new feature in Cellular 3.0. The essence is that the compiler automatically generates an anonymous class according to our custom to help us store temporary variables. Anonymous types also depend on another feature: support for creating objects based on property. For example, var d = new {Name = "s"}; the compiler automatically generates an anonymous class with property called Name, allocates memory according to that type, and initializes the object. But var d = new {"s"}; is compiled and failed. Because the compiler does not know the name of the property in the anonymous class. For example, string c = "d"; var d = new {c}; can be compiled. The compiler creates a property called anonymous class with a name of c.
For example, the following example: new {cmagnetic ContactName c. Phone}; both ContactName and Phone define the property corresponding to the fields in the table in the mapping file. When the compiler reads the data and creates the object, it creates an anonymous class with two properties, ContactName and Phone, and initializes the object based on the data. In addition, the compiler can rename property.
Var Q = from c in db.Customers select new {c.ContactName, c.Phone}
Sentence description: query the customer's contact person and telephone number.
Var Q = from e in db.Employees select new {Name = e.FirstName + "" + e.LastName, Phone = e.HomePhone}
Sentence description: query the employee's name and home phone number
Var Q = from p in db.Products select new {p.ProductID, HalfPrice = p.UnitPrice / 2}
3. Conditional form:
Description: generate SQL statement as: case when condition then else.
Var Q = from p in db.Products select new {p.ProductName, Availability = p.UnitsInStock-p.UnitsOnOrder < 0? "Out Of Stock": "In Stock"}; this is all the content of the article "how to implement simple form, anonymous type form and conditional form in Select in LINQ to SQL". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.