'Adding one linq line to my function calls
I am trying to add a linq line to a MapField (Google protobuf).
My actual code works fine like
private static MapField<string, TestResultProto> dToP (Dictionary<Uri, TestResult> keyValuePairs)
{
MapField<string, TestResultProto> keyValues = new();
foreach (var pair in keyValuePairs)
{
keyValues[pair.key.ToString()] = TRTP (pair.value);
}
return keyValues;
}
private static TestResultProto TRTP (TestResult tr)
{
TestResultProto t = new()
{
id = tr.id;
email = tr.email;
};
return t;
}
And now I use the above methods.
object o = //something;
Class1 c = new Class1();
c.Results.Add(dToP(o.Results)); // This works as expected
//c.Results is a MapField<string, TestResultProto>
//o.Results is Dictionary<Uri, TestResult> keyValuePairs
So, to write the above line, I was thinking to add a linq like :
**c.Results.AddRange(o.Results.Select(TRTP))); //ERROR**
Here, I get the error in the linq (above line), saying no method for AddRange for MapField. So, there is something like AddEntriesFrom, but the signature is it expects Codec. Any idea on how to do it ?
Solution 1:[1]
If you're desperate to turn it LINQ you could do something like:
private static MapField<string, TestResultProto> dToP (Dictionary<Uri, TestResult> d)
{
var m = new MapField<string, TestResultProto>();
m.Add(d.ToDictionary(
kvp => kvp.Key.ToString(),
kvp => new TestResultProto
{
id = kvp.Value.id,
email = kvp.Value.email
}
));
return m;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Caius Jard |
