Reputation: 6265
how can i write to a file inside a linq query? or call a custom function that writes to a file? i'm comparing pictures for similarity. and want to check the files that have been compared to each other in a log file. because some pictures where marked as similar but aren't.
thanks in advance!
[EDIT]
the linq query is just:
IEnumerable<PicInfo> t = from f in lista
from q in listb
where (((64 - BitCount(f.visualCharHash ^ q.visualCharHash)) * 100.0) / 64.0 == 100)
no writings to a file are being made here. simply, because i have no clue how to do so. and google didnt find me any results showing how to do this.
but it does show 'writing linq query's'
no results for 'write to file inside linq query's'.
[/EDIT]
Upvotes: 3
Views: 2132
Reputation: 120518
Well, LINQ is meant for querying only (i.e. code that has no side-effects), however, with LINQ to Objects, it's easily possible to subvert it to write your logs if required... for instance:
myEnumerable.Select( m => m.SomeProp )
could be changed to:
myEnumerable.Select( m => {
myLogger.Log(m.SomeProp);
return m.SomeProp;
})
I wouldn't recommend this for any purpose other than debugging.
So, to do this with your query, you'd need to convert to method chains:
lista
.SelectMany(i => listb, (f, q) => new {f, q})
.Where(x => {
var v = (((64 - BitCount(x.f.visualCharHash ^ x.q.visualCharHash))
* 100.0)
/ 64.0;
//logging?
return v == 100;
})
Upvotes: 5
Reputation: 24413
HashSet<int> x = new HashSet<int>();
x.Add(1);
x.Add(2);
string filename = "E:\\foo.txt";
using (TextWriter myStreamWriter = new StreamWriter(filename))
{
x.ToList().ForEach(dd => myStreamWriter.WriteLine(dd));
}
You can convert to List and use ForEach
Upvotes: 1