LINQ to able

Source: Internet
Author: User

There are a lot of things worth learning about in LINQ. Here we will mainly introduce LINQ to able, including the datatable type and other aspects.

LINQ to able

The difficulty of query is no longer felt by LINQ, which makes it easy for us to implement difficult queries. Maybe the remaining question may be a very simple question: how to save these query results?

 
  
  
  1. VaR _ result = Dal. Utility. selectall<Customer>();
  2. VaR _ filter =
  3. From Q in _ result
  4. Where Q. customerid. startswith ("B ")
  5. Select New
  6. {
  7. Q. customerid,
  8. Q. contactname,
  9. Q. companyName
  10. };

_ Result is the result of searching all customers. _ filter is the {customerid, contactname, companyName} set of customerid starting with "B" in _ result, however, this anonymous type cannot be returned to another method as a variable.

This {customerid, contactname, companyName} collection may be reflected to a able type, which is more suitable for general database requirements. It may also be a solution for some old systems to easily plug in the wings of LINQ.

The following shows the method for writing data to a LINQ able (which is the same in some sense ):

 
 
  1. Public static system. Data. datatable linqtodatatable<T>(Ienumerable<T>Data)
  2. {
  3. VaR dt = new system. Data. datatable ();
  4. VaR PS = typeof (T). getproperties (). tolist ();
  5. PS. foreach (P =>DT. Columns. Add (P. Name, P. propertytype ));
  6. Foreach (t in data)
  7. {
  8. VaR DR = DT. newrow ();
  9. VaR Vs = from P in PS select P. getvalue (T, null );
  10. VaR ls = vs. tolist ();
  11. Int I = 0;
  12. Ls. foreach (C =>Dr [I ++] = C );
  13. DT. Rows. Add (DR );
  14. }
  15. Return DT;
  16. }
  17. Public static system. Data. datatable todatatable<T>(Ienumerable<T>Data)
  18. {
  19. VaR dt = new system. Data. datatable ();
  20. VaR PS = system. componentmodel. typedescriptor. getproperties (typeof (t ));
  21. Foreach (system. componentmodel. propertydescriptor DP in PS)
  22. DT. Columns. Add (DP. Name, DP. propertytype );
  23. Foreach (t in data)
  24. {
  25. VaR DR = DT. newrow ();
  26. Foreach (system. componentmodel. propertydescriptor DP in PS)
  27. Dr [DP. Name] = DP. getvalue (t );
  28. DT. Rows. Add (DR );
  29. }
  30. Return DT;
  31. }

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.