Reputation: 1312
In the below (pasted from LinqPad) my (very silly) query return no results. Why not, what is wrong? (please someone sort the formatting)
void Main()
{
var csv =
@"#Books (format: ISBN, Title, Authors, Publisher, Date, Price)
0735621632,CLR via C#,Jeffrey Richter,Microsoft Press,02-22-2006,59.99
0321127420,Patterns Of Enterprise Application Architecture,Martin Fowler,Addison-Wesley, 11-05-2002,54.99
0321200683,Enterprise Integration Patterns,Gregor Hohpe,Addison-Wesley,10-10-2003,54.99
0321125215,Domain-Driven Design,Eric Evans,Addison-Wesley Professional,08-22-2003,54.99
1932394613,Ajax In Action,Dave Crane;Eric Pascarello;Darren James,Manning Publications,10-01-2005,44.95";
using (var reader = new StringReader(csv) /*new StreamReader("books.csv")*/)
{
var books =
from line in reader.Lines()
where !line.StartsWith("#")
let parts = line.Split(',')
select new { Isbn=parts[0], Title=parts[1], Publisher=parts[3] };
//books.Dump();
var query =
from book in books
from b in books
where book.Isbn == b.Isbn
select new
{
book.Isbn,
book.Title
};
query.Dump();
// Warning, the reader should not be disposed while we are likely to enumerate the query!
// Don't forget that deferred execution happens here
}
}
}// Temporary hack to enable extension methods
/// <summary>
/// Operators for LINQ to Text Files
/// </summary>
public static class Extensions
{
public static IEnumerable<String> Lines(this TextReader source)
{
String line;
if (source == null)
throw new ArgumentNullException("source");
while ((line = source.ReadLine()) != null)
yield return line;
}
Upvotes: 1
Views: 288
Reputation: 1052
.ToList() should do the job. This works:
void Main()
{
var csv =
@"#Books (format: ISBN, Title, Authors, Publisher, Date, Price)
0735621632,CLR via C#,Jeffrey Richter,Microsoft Press,02-22-2006,59.99
0321127420,Patterns Of Enterprise Application Architecture,Martin Fowler,Addison-Wesley, 11-05-2002,54.99
0321200683,Enterprise Integration Patterns,Gregor Hohpe,Addison-Wesley,10-10-2003,54.99
0321125215,Domain-Driven Design,Eric Evans,Addison-Wesley Professional,08-22-2003,54.99
1932394613,Ajax In Action,Dave Crane;Eric Pascarello;Darren James,Manning Publications,10-01-2005,44.95";
using (var reader = new StringReader(csv) /*new StreamReader("books.csv")*/)
{
var books =
(from line in reader.Lines()
where !line.StartsWith("#")
let parts = line.Split(',')
select new { Isbn=parts[0], Title=parts[1], Publisher=parts[3] }).ToList();
//books.Dump();
var query =
from book in books
from b in books
where book.Isbn == b.Isbn
select new
{
book.Isbn,
book.Title
};
query.Dump();
// Warning, the reader should not be disposed while we are likely to enumerate the query!
// Don't forget that deferred execution happens here
}
}
}// Temporary hack to enable extension methods
/// <summary>
/// Operators for LINQ to Text Files
/// </summary>
public static class Extensions
{
public static IEnumerable<String> Lines(this TextReader source)
{
String line;
if (source == null)
throw new ArgumentNullException("source");
while ((line = source.ReadLine()) != null)
yield return line;
}
Upvotes: 2
Reputation: 134841
You attempt to enumerate through the books
collection twice in your second query. This would not work as-is however since your Lines()
extension method reads through the reader to the end at first. Due to the deferred execution, the second enumeration attempts to read off of the now empty reader leading to no results.
You need to put the results of the books
collection into a list (or other collection) so it's not trying to access the reader again. In your case, you could just call ToList()
on your books collection and it should work from there.
var books =
(from line in reader.Lines()
where !line.StartsWith("#")
let parts = line.Split(',')
select new { Isbn = parts[0], Title = parts[1], Publisher = parts[3] })
.ToList();
Upvotes: 4
Reputation: 7304
The "Lines" method implies your source is built by 1+ strings separated by a new-line termination. I don't think that the above C# code would recognize the termination as you expect.
Upvotes: 0