How to read a CSV file into a .NET Datatable

I have been using OleDb provider. However, it has problems if you are reading in rows that have numeric values but you want them treated as text. However, you can get around that issue by creating a schema.ini file. Here is my method I used: // using System.Data; // using System.Data.OleDb; // using System.Globalization; // … Read more

LINQ query on a DataTable

You can’t query against the DataTable‘s Rows collection, since DataRowCollection doesn’t implement IEnumerable<T>. You need to use the AsEnumerable() extension for DataTable. Like so: var results = from myRow in myDataTable.AsEnumerable() where myRow.Field<int>(“RowNo”) == 1 select myRow; And as @Keith says, you’ll need to add a reference to System.Data.DataSetExtensions AsEnumerable() returns IEnumerable<DataRow>. If you need … Read more

How and when should I load the model from database for h:dataTable

Do it in bean’s @PostConstruct method. @ManagedBean @RequestScoped public class Bean { private List<Item> items; @EJB private ItemService itemService; @PostConstruct public void init() { items = itemService.list(); } public List<Item> getItems() { return items; } } And let the value reference the property (not method!). <h:dataTable value=”#{bean.items}” var=”item”> In the @PostConstruct you have the advantage … Read more

Convert generic List/Enumerable to DataTable?

Here’s a nice 2013 update using FastMember from NuGet: IEnumerable<SomeType> data = … DataTable table = new DataTable(); using(var reader = ObjectReader.Create(data)) { table.Load(reader); } This uses FastMember’s meta-programming API for maximum performance. If you want to restrict it to particular members (or enforce the order), then you can do that too: IEnumerable<SomeType> data = … Read more

How can I pass selected row to commandLink inside dataTable or ui:repeat?

As to the cause, the <f:attribute> is specific to the component itself (populated during view build time), not to the iterated row (populated during view render time). There are several ways to achieve the requirement. If your servletcontainer supports a minimum of Servlet 3.0 / EL 2.2, then just pass it as an argument of … Read more

Affect the datatable column value in C#

You can use Linq-To-DataTable especially GroupBy: var newNames = table.AsEnumerable() .Select((row, rowIndex) => new { row, rowIndex }) // storing row and index of it in anonymous type .GroupBy(x => x.row.Field<string>(“Name”)) // group by Name .Where(g => g.Count() > 1) // take only duplicates .SelectMany(g => g // select all rows but first of duplicates … Read more

How to use Select method of DataTable

Yes, this works. For a list of all possible expressions see http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx Here also is a sample program demonstrating this works. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { DataTable table = new DataTable(); // Create the first column. DataColumn textColumn = … Read more