line number while querying with linq - c#

I am using a stream reader to read a text file and then using Linq for retrieving the information
String fileContent = prodFileStreamReader.ReadToEnd();
var mydata = from con in fileContent.Split('$').Select(x => x.Trim())
where !String.IsNullOrEmpty(con)
select new BaseSegment
{
dataID = con.Substring(0, con.IndexOf('#')),
dataElms = con.Split('#').ToArray(),
dataCon = con,
lineNumber =
};
I would also like to get the line number. I tried using Index but I was not able to. How to query to get the index and assign it to lineNumber?

Try using the select that projects index into each item, as given in this msdn article: http://msdn.microsoft.com/en-us/library/bb534869.aspx
In your case something like this (not tested):
var mydata = fileContent.Split('$')
.Select(x => x.Trim())
.Where(con => !String.IsNullOrEmpty(con))
.Select((con, index) => new
{
dataID = con.Substring(0, con.IndexOf('#')),
dataElms = con.Split('#').ToArray(),
dataCon = con,
lineNumber = index
});

For starters, I would not read the file in as a big string. Use methods that could process it in small chunks. Use File.ReadLines() for example to read through the file line by line. It will be easier to get line numbers this way and much more efficient than reading it all at once only to split it up again.
const string filePath = ...;
var myData =
from pair in File.ReadLines(filePath)
.Select((LineNumber, Line) => new { LineNumber, Line })
where ...
select new BaseSegment
{
...
Line = pair.Line,
LineNumber = pair.LineNumber,
};
p.s., You should stick to the usual C# naming conventions. Public properties of your classes should use PascalCasing, not camelCasing and should not be abbreviated.
The code you use to process the content looks awkward. It could probably be improved if I knew what the files looked like. I'll leave that out until you could show us how it is.

How about this?
var animalList = from a in animals
select new { Animal = a, Index = animals.IndexOf(a) };
or in your case...
Index = fileContent.IndexOf(con)

if this whole data is going in myData then you can use the index directly from the myData.

Try This:
String fileContent = prodFileStreamReader.ReadToEnd();
var mydata = from con in fileContent.Split('$').Select(x => x.Trim())
where !String.IsNullOrEmpty(con)
select new BaseSegment
{
dataID = con.Substring(0, con.IndexOf('#')),
dataElms = con.Split('#').ToArray(),
dataCon = con,
lineNumber = Array.IndexOf(fileContent.Split('$').Select(x => x.Trim(),con)
};
Array.IndexOf(yourArrey,the string you looking for); -> will return the index in the arrey.

you can try something like this
long index = 0;
var xElementsAndNodes = from xmlElement in elementsColl
select new
{
Index = index += 1,
....
}

Related

C# separating data within a line from a text file

I have a data file
Name; LastName; EurosCents;
Name2; LastName2; EurosCents2;
(for example:
John; Smith; 4,20;
Josh; Peck; 6,50;
)
I need to read the data and then do some further work with it... Is there any way to read the lines and save them? As the only way to read from a text file is to read the entire line at once.
var lst = File.ReadAllLines(yourFilePath).Select(x => new
{
FirstName = x.Split(';')[0]
LastName = x.Split(';')[1]
Value = decimal.Parse(x.Split(';')[2])
}).ToList();
use
lst[7].FirstName = "xxx";
Console.WriteLine(lst[2].Value);
etc...
The File API provide multiple options for reading files. Below is a possible way to proceed:
foreach(var line in File.ReadAllLines(path))
{
var splitted = line.Split(';');
var name = splitted.ElementAtOrDefault(0);
var lastName = splitted.ElementAtOrDefault(1);
var cents = Decimal.Parse(splitted.ElementAtOrDefault(2));
}
Parsing will be very easy if you are comfortable using LINQ.
Below line can get you the file in a hierarchical structure.
var theselines = File.ReadLines(#"C:\Test.txt").Select(l => l.Split(','));
You can see the result of above line by debugging.
Later you can have any logic to get the required data from each line without using foreach loop.
var Data = theselines.Select(l => new
{
id = l.Where(t => t.Contains("01")).FirstOrDefault(),
Price = l.Where(t => t.Contains(",")).FirstOrDefault(),
Firstname= l[0],
lastname = l[1]
});
Providing that data itself (both names and cents) can't contain ; in order to get
items from the comma separated values you can just split:
var data = File
.ReadLines(#"C:\MyData.csv")
// .Skip(1) // <- in case you have caption to skip
.Select(line => line.Split(';'))
.Select(items => new {
Name = items[0],
LastName = items[1],
EuroCents = decimal.Parse(items[2]) //TODO: check type and its format
});
//.ToArray(); // <- if you want to materialize as, say, an array
Then you can use it
foreach (var item in data) {
if (item.EuroCents > 10) {
...
}
}
the simple code to read whole file is as follows
string[] test(string path)
{
System.IO.StreamReader sr = new System.IO.StreamReader(path);
string[] str = sr.ReadToEnd().Split(';');
sr.Close();
return str;
}

Linq over InputStream from HttpPostedFileWrapper

Is it possible to apply a Linq query from a HttpPostedFileWrapper?
My web app allows users to select a bunch of .csv files. I now need to open those files and import them.
My previous code, which uses paths and file names looks like;
importedList = (from csvLine in File.ReadAllLines(fileName)
let x = csvLine.Split(',')
select new ImportedXDock
{
StoreNumber = int.Parse(x[0]),
DCNumber = int.Parse(x[1]),
DeliveryDay = x[2],
Activity = x[3],
ActivityDay = x[4],
Time = TimeSpan.Parse(x[5])
}).ToList();
However, now that i have a collection of HttpPostedFileWrapper objects how would I do the same?
edit
Or do I need to convert it to something and then read the file?
You may be able to loop over the file names instead of the input streams
foreach (var fileName in wrapper.Select(w => w.FileName))
{
yield return (from csvLine in File.ReadAllLines(fileName)
let x = csvLine.Split(',')
select new ImportedXDock
{
StoreNumber = int.Parse(x[0]),
DCNumber = int.Parse(x[1]),
DeliveryDay = x[2],
Activity = x[3],
ActivityDay = x[4],
Time = TimeSpan.Parse(x[5])
}).ToList();
}

How to compare and select values from string using Linq?

Had a class:
class filedate
{
public int id;
public string fname;
}
Fill my list with values:
List<filedate> List = ReadList(sqlFiles);
string[] FolderFiles = System.IO.Directory.GetFiles(path2Copy);
Trying to get results:
var results = List.Where(filedate =>
FolderFiles.Any(x=>Path.GetFileNameWithoutExtension(x) ==
Path.GetFileNameWithoutExtension(filedate.fname)));
I have the same files in List and FolderFiles, but get no results in results. I am a newbie to Linq. Where is the problem?
update:
List: (count) > 1000
for example:
<1023, 'tr_F2opervag_2808_1644.dat'>
FolderFiles example:
"\\domain.corp.dns\share\folder\tr_F2opervag_2808_1644.dat"
Update 2:
found out my mistake! Comment with intersection was helpful! This code is working:
var results = List.Where(
(filedate x) =>
{
return ! FolderFiles.Any(xxx =>
Path.GetFileNameWithoutExtension(xxx) ==
Path.GetFileNameWithoutExtension(x.fname));
});
You're code works fine for me so there's something wrong with the format of your data in the List coming back from the database.
Post an example of an fname value from the filedata object. It needs to be a valid fully qualified path.
This works fine for me.
public class FileData{
public int id;
public string fname;
}
void Main()
{
List<FileData> list = new List<FileData>{
new FileData { id=1, fname="C:\\install.res.1042.dll"},
new FileData { id=2, fname="C:\\install.res.1041.dll" },
new FileData { id=3, fname="C:\\install.res.9999.dll"}
};
string[] FolderFiles = System.IO.Directory.GetFiles("C:\\");
var results = list
.Where(fd =>
FolderFiles.Any(x=>Path.GetFileNameWithoutExtension(x) ==
Path.GetFileNameWithoutExtension(fd.fname)));
Console.WriteLine(results);
}
If you need to find the difference this should work. This is available via Enumerable.Except.
var dbFiles = ReadList(sqlFiles);
var dbFilePaths =
dbFiles.Select(fdate =>
Path.GetFileNameWithoutExtension(fdate.fname).ToLower());
var fsFilePaths =
Directory
.GetFiles(path2Copy)
.Select(filePath =>
Path.GetFileNameWithoutExtension(filePath).ToLower());
var diff =
dbFilePaths
.Except(fsFilePaths)
.Join(dbFiles,
filePath => filePath,
fdate => fdate.fname,
(filePath, fdate) => fdate)
.ToList();

c# find keywords in a list and create new list from the found items

i am learning c# and have the following problem, i can not find a solution.
the code i am trying is:
string theString = "aaa XXX,bbb XXX,ccc XXX,aaa XXX";
List<string> listFromTheString= new List<string>(theString.Split(','));
List<string> listOfFoundItems = new List<string>();
for (int i = 0; i < (listFromTheString.Count); i++)
{
if(listFromTheString[i].Contains("aaa"))
{
listOfFoundItems.Add(listFromTheString[i]);
}
}
I would like to iterate through the list and create new items in a new list if a special keyword is found. The list listOfFoundItems does not get filled with the founds.
can you please give me a hint what i am doing wrong?
You can accomplish this more succinctly with LINQ:
string theString = ("aaa XXX,bbb XXX,ccc XXX,aaa XXX");
List<string> listFromTheString = new List<string>(theString.Split(','));
List<string> listOfFoundItems = listFromTheString.Where(s => s.Contains("aaa")).ToList();
The code you provided does work, though.
Here's an alternate, one-line version:
List<string> listOfFoundItems = theString.Split(',').Where(s => s.Contains("aaa")).ToList();
theString.Split(',').Where(p=>p.Contains("aaa")).ToList()
I know you want to fix your algorithm, but once you do, consider grokking this expression:
listofFoundItems = (from s in theString.Split(',')
where s.Contains("aaa")
select s).ToList();
The code you provide works fine. Given that, I suspect you may be having some string comparison issues.
This code may work better for you:
const string given = "aaa XXX,bbb XXX,ccc XXX,aaa XXX";
var givenSplit = new List<string>(given.Split(','));
var listOfFoundItems = new List<string>();
foreach(var item in givenSplit.Where(g => g.IndexOf("aAa", StringComparison.InvariantCultureIgnoreCase) > -1))
{
listOfFoundItems.Add(item);
}
// two items are added
string theString = ("aaa XXX,bbb XXX,ccc XXX,aaa XXX");
List<string> listFromTheString = new List<string>(theString.Split(','));
List<string> listOfKeywords = new List<string> { "aaa" };
List<string> found = (from str in listFromTheString
where listOfKeywords.Any(keyword => str.Contains(keyword))
select str).ToList<string>();

How to create LINQ QUERY dynamically?

Is there any way to insert part of the code between { } dynamically?
LINQ QUERY:
var csvdata = from csvline in csvlines
let column = csvline.Split(';')
select new {
produkt = column[0],
cislo = column[1],
part = column[2],
serial = column[3]
};
I mean something like:
string qpart = "produkt = column[0], cislo = column[1], part = column[2], serial = column[3]";
var csvdata = from csvline in csvlines
let column = csvline.Split(';')
select new {
qpart
};
Thanks for answers..
Try investigating Dynamic Linq Query Library.
http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
http://naspinski.net/post/Writing-Dynamic-Linq-Queries-in-Linq-to-Entities.aspx
You'll need to convert the string array returned from Split into IQueryable for it to work but I think this is your best shot.
var results = columns
.Select("new(column[0] As produkt)");
Is how I'd I imagine it would work?.

Categories