Amazing use of cast: general use of the LINQ sentence

Source: Internet
Author: User

Cast Wonderful use: General Use LINQ SentenceWen/min zhongcheng What is generic LINQ? SentenceIn general, the concept of using a LINQ sentence is very simple. In some cases, we may need to use the same program to query different data tables. This is in ADO. net is easy to parse, see the following example:

Static void printcustomerid (sqldatareader reader) {console. writeline (reader. getstring (reader. getordinal ("customerid ")));}
This function accepts a reader object, and then prints the merid vertex value. This is not limited to the schema or data table selected by the sqldatareader, as long as the schema contains the customerid iterator. However, this method does not have a very direct coding method in the typed-query (with special check sentence) mode of LINQ, because you cannot explain the following sentence.

Static void Test2 (INT index ){VaR query;Dataclasses1datacontext context = new dataclasses1datacontext (); context. log = console. out; // for log only, you can remove it. if (Index = 1) query = context. customers; else query = context. orders; var result = From S1 in query where s1.customerid. contains ("v") Select S1; foreach (VAR item in result) {console. writeline (item. customerid );}}
The hacker will complain that the changes of VaR must be specified at the time of declaration. So how can we achieve the same effect in the LINQ to SQL or LINQ to entites collections? There are several ways to do this.

1. Use executequery and another typed object to receive the response set.
2. Use entity class to accept.
3. Use cast and partial class.
1 and 2 should be avoided for beginners who are familiar with LINQ to SQL, so I will not explain it again. The third method is rarely seen, we can use the partial class mechanism to implement the entity classes interface, and then use the cast function to explain the purpose.

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. data. LINQ; using system. data. sqlclient; namespace consoleapplication39 {class program {static void main (string [] ARGs) {test (1); console. writeline ("------"); test (0); console. readline ();} static void test (INT index) {iqueryable <igenericbaseclass> query = NULL; dataclasses1datacontext context = new dataclasses1datacontext (); context. log = console. out; // for log only, you can remove it. if (Index = 1) query = context. MERs. cast <igenericbaseclass> (); else query = context. orders. cast <igenericbaseclass> (); var result = From S1 in query where s1.mermerid. contains ("v") Select S1; foreach (VAR item in result) {console. writeline (item. customerid) ;}} public interface igenericbaseclass {string customerid {Get; Set ;}} partial class MERs: igenericbaseclass {} partial class orders: igenericbaseclass {}}
I believe that you will find a different method for using the above Code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.