简体   繁体   中英

C# Querying a datatable

I have data in excel and I have transformed it into a datatable so that I can use it to query with sql statements. But querying datatable in c-sharp is not straightforward. Can anyone suggest a way to temporary read data from excel file hold it in memory, ask some queries hold results in datagridview and delete data from memory.

You can query against the rows in a DataTable by using Linq.

This can be found using a Google search for examples.

What is Linq?

Linq was/is developed by Microsoft as a programming model that gives you the ability to perform queries within the .net. Linq can also be used on multiple sources, such as DataTables, SQL Databases, arrays and more. The great thing about Linq is that it uses SQL-like syntax to make it understandable for Database Developers and Software Developers. However, do note that it is not as powerful as SQL due to only supporting less advanced techniques, however you can find out more about this when researching.

Linq Example

This example of Linq queries an array called words where the word length is = 3 and returns them.

from word in words where word.length == 3 select word;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM