score:2
Accepted answer
NOTE: This solution is based on the file shared in question. Data is separated by spaces and format is not advisable to use. Answering to help person with content format he has. Tested and working.
static void Main(string[] args)
{
List<Data> list = new List<Data>();
var dd = File.ReadAllLines(@"C:\Users\XXXX\Desktop\test.txt")
.Skip(1)
.Where(s => s.Length > 1).ToList();
foreach (var item in dd)
{
var columns = item.Split('\t').Where(c => c.Trim() != string.Empty).ToList();
if (columns != null && columns.Count > 0)
{
int id;
if (int.TryParse(columns[0], out id))
{
list.Add(new Data()
{
id = Convert.ToInt32(columns[0]),
Name = columns[1],
Description = columns[2],
Quantity = Convert.ToInt32(columns[3]),
Rate = Convert.ToDouble(columns[4]),
Discount = Convert.ToInt32(columns[5]),
Amount = int.Parse(columns[6])
});
}
else
{
list.Last().Description += columns[0];
}
}
}
Console.ReadLine();
}
Source: stackoverflow.com
Related Articles
- How to Read This Text File and store in a list using C#
- Read text data from file using LINQ
- Read text file word-by-word using LINQ
- Read xml using LINQ and store in a list of objects
- read text file and writing to a list
- Construct a list of wpf Hyperlink elements from an XML source file using Linq
- Get numerical data from a tab-separated text file and store them in a list
- How to Read from a Text File into a List And Read from that list C#
- Read a text file using LinQ and then insert it into SQL Table using LinQ To SQL
- How do I find the text within a div in the source of a web page using C#
- read xml file using linq
- How to read File names recursively from subfolder using LINQ
- How to read XML file using System.IO.Stream with LINQ
- How to read from a Text File Faster/Smarter?
- Read text file into dictionary without duplicates
- How can I read a tab delimited file into a multidimensional array using Linq?
- Read first 10 line from file using LINQ
- C# .Net 3.5 Code to replace a file extension using LINQ
- using LINQ to process a text file
- How to extract text from HTML using htmlagilitypack for this sample?
- Creating a list of no duplicate string entries in a text file at C#
- C# how can I sort a list of file by index using linq
- Parsing a text File using Linq
- Using LINQ to query a text file
- reversing text from input file using linq
- Read from xml file to objectlist using Linq
- read icollection data using LINQ in C# code
- File Upload read to memory and use as text file - is there a better way?
- How can I get totals for more than one column in a list and put this into an object using LINQ?
- How to read 3 lines of a text file c#
- Matching two Lists of different types together
- Dynamic Linq select first in groups after GroupBy
- Linq lambda arguments
- How to i get the Value of Store procedure Output value in Entity Frame Work?
- Video Sitemap colon in name using LINQ to XML
- Insert XAttribute to existing XElement at specified position
- Finding duplicate texts in IEnumerable<TextBox> collection
- How group list elements by parent ID like tree in c# MVC4 application?
- I have a list of countries. I'd like to sort it alphabetically, except for two countries which I'd like to put first
- Get the no of weeks from date duration with overlapping scenario
- CompilationErrorException when including System.Linq in Roslyn CP2
- Getting the sum of a value in Linq
- Linq: select from multiple tables into one pre-defined entity
- Linq is returning too many results when joined
- How to show a sub object property in GridView with LINQ
- convert a linq statement as expression
- vb.net Linq statement to find duplicates using fuzzy searching
- LINQ Statement Optimization - Seeking advice for better design
- iterate through two lists
- What is the best way to combine multiple DbQuery results?