I am trying to use LINQ To Objects to create a query that will give me files indexed by file name, matching values ββwith their binary data like byte[].
byte[]
However, I cannot find a "neat" way to do this. I hope to get something like output Dictionary<T,K>.
Dictionary<T,K>
Here is what I still have. Example delimFileNames = "1.jpg | 2.jpg"
//Extract filenames from filename string //and read file binary from file //select result into a filename indexed collection var result = from f in delimFileNames.Split(Constants.DDS_FILENAME_SEPARATOR) let filePath = Path.Combine(ddsClient.WorkingDirectory, f) let fileData = File.ReadAllBytes(filePath) select new KeyValuePair<string, byte[]>(f, fileData); return result.ToDictionary(kvp => kvp.Key, kvp=> kvp.Value);
The main scraper for the head is why I can't use bottomless ToDictionary () or live translation. Any suggestions or alternatives to improve the above are appreciated.
ToDictionary() KeyValuePair - , :
ToDictionary()
public static Dictionary<TKey, TValue> ToDictionary<TKey, TValue>( IEnumerable<KeyValuePair<TKey, TValue>> source) { return source.ToDictionary(kvp => kvp.Key, kvp=> kvp.Value); }
:
var result = from f in delimFileNames.Split(Constants.DDS_FILENAME_SEPARATOR) .ToDictionary(f => f, // Outdented to avoid scrolling f => File.ReadAllBytes(Path.Combine(ddsClient.WorkingDirectory, f));
"let", select , ( ) .
select
" - , ToDictionary() [...]"
ToDictionary , . Microsoft , , , . , .
ToDictionary
"[...] .
Linq IEnumerable<KeyValuePair<TKey, TValue>>, Dictionary<TKey, TValue>. /, .
IEnumerable<KeyValuePair<TKey, TValue>>
Dictionary<TKey, TValue>
, Dictionary<TKey, TValue> IS a IEnumerable<KeyValuePair<TKey, TValue>> ( ), , / , Linq, - , .
KeyValuePair<,>, , , Key Value:
KeyValuePair<,>
Key
Value
var files = from fileName in delimFileNames.Split(Constants.DDS_FILENAME_SEPARATOR) let filePath = Path.Combine(ddsClient.WorkingDirectory, fileName) select new { Path = filePath, Data = File.ReadAllBytes(filePath) }; return files.ToDictionary(file => file.Path, file => file.Data);
Source: https://habr.com/ru/post/1717351/More articles:Is there a publicly available HTTPS POST smoke test? - language-agnosticSo, .NET has no built-in Office features? - .nettracking changes in database structure - databaseC # retrieves where where on linq - c #How to check the status of an access form 2003, modal or model? - vbaGridView in ASP.net using the wrong MM / DD / YYYY date format instead of DD / MM / YYYY - asp.netPHP - SESSION multidimensional array - good or bad idea for performance? - arraysPDOs don't run the same queries twice? - phpA few questions about x86 - assemblyIs the System.MessageQueue (MSMQ) message lost if my function does not work while processing it? - c #All Articles