Mapping complex object to dictionary using LINQ - c#

Consider the following object:
Controller controller = new Controller()
{
Name = "Test",
Actions = new Action[]
{
new Action() { Name = "Action1", HttpCache = 300 },
new Action() { Name = "Action2", HttpCache = 200 },
new Action() { Name = "Action3", HttpCache = 400 }
}
};
How can I map this object to a dictionary of the following form?
#key# -> #value#
"Test.Action1" -> 300
"Test.Action2" -> 200
"Test.Action3" -> 400
That is, a Dictionary<string, int>.
I am interested in a LINQ solution but I can't manage to work around it.
I am trying to map each action to a KeyValuePair, but I don't know how to get each action's parent controller's Name property.

The main thing is the controller is still in scope in the lambda:
var result = controller.Actions.ToDictionary(
a => string.Format("{0}.{1}", controller.Name, a.Name),
a => a.HttpCache);

The LINQ approach is to project the Actions list, using the Select method, into a dictionary. Since you're calling it on a Controller instance, you have access to the Controller's Name as well:
myController.Actions.ToDictionary(
/* Key selector - use the controller instance + action */
action => myController.Name + "." + action.Name,
/* Value selector - just the action */
action => action.HttpCache);
If you want to make one large dictionary from several controllers, you can use SelectMany to project each Controller's items into a list of Controller+Action, then convert that list into a dictionary:
var namesAndValues =
controllers.SelectMany(controller =>
controller.Actions.Select(action =>
{
Name = controller.Name + "." + action.Name,
HttpCache = action.HttpCache
}));
var dict = namesAndValues.ToDictionary(nav => nav.Name, nav => nav.HttpCache);

You can try this :
var dico = controller.Actions
.ToDictionary(a => $"{controller.Name}.{a.Name}",
a => a.HttpCache);
The first lambda expression target the key whereas the second target the value of the dictionary entry.

Assuming that you have multiple controllers in a collection, not just the one controller variable in your example code, and want to put all of their actions into a single dictionary, then you could do something like this:
var httpCaches = controllers.SelectMany(controller =>
controller.Actions.Select(action =>
new
{
Controller = controller,
Action = action
})
)
.ToDictionary(
item => item.Controller.Name + "." + item.Action.Name,
item => item.Action.HttpCache);
This would be for the case where you have your data set up like this:
var controllers = new[] {
new Controller()
{
Name = "Test1",
Actions = new Action[] {
new Action { Name = "Action1", HttpCache = 300 },
new Action { Name = "Action2", HttpCache = 200 },
new Action { Name = "Action3", HttpCache = 400 },
}
},
new Controller()
{
Name = "Test2",
Actions = new Action[] {
new Action { Name = "Action1", HttpCache = 300 },
new Action { Name = "Action2", HttpCache = 200 },
new Action { Name = "Action3", HttpCache = 400 },
}
},
};

Related

Need to add Parameters to NewActionConfiguration but cant use a foreach inside the definition

Using Axis Communications VAPIX WSDL APIs - I'm setting up a NewActionConfiguration which takes a number of parameters that I have saved in a List but the way the API documents have the implementation I cant loop through my parameter list XML objects while defining the newAction object.
//This is how the API docs say to do it:
NewActionConfiguration newAction = new NewActionConfiguration
{
TemplateToken = overlayToken,
Name = "Overlay Text",
Parameters = new ActionParameters
{
Parameter = new[]
{
new ActionParameter { Name = "text", Value = "Trigger:Active" },
new ActionParameter { Name = "channels", Value = "1" },
new ActionParameter { Name = "duration", Value = "15" }
}
}
};
//This is what I need to do:
NewActionConfiguration newAction = new NewActionConfiguration
{
Name = xmlPrimaryAction["Name"].InnerText,
TemplateToken = xmlPrimaryAction["ActionTemplate"].InnerText,
Parameters = new[]
{
foreach (ActionParameter actionParameter in actionParameterList)
{
new ActionParameter { Name = actionParameter.Name, Value = actionParameter.Value };
}
}
};
The API will not allow me to just do a: newAction.Parameters.Parameter.Add(actionParameter) or the like. Anyone got any ideas?
So first things, you cannot declare a foreach block inside an object instantiating scope block. What you need to do is declare a variable before, in the function scope and then attribute the Parameter property to it. Like so:
var actionParameters = new List<ActionParameter>();
foreach (ActionParameter actionParameter in actionParameterList)
{
actionParameters.Add(new ActionParameter { Name = actionParameter.Name, Value = actionParameter.Value });
}
NewActionConfiguration newAction = new NewActionConfiguration
{
Name = xmlPrimaryAction["Name"].InnerText,
TemplateToken = xmlPrimaryAction["ActionTemplate"].InnerText,
Parameters = actionParameters.ToArray()//Use System.Linq here to convert the list into an array
};
Found it! Thanks for the help #Vitor you were close but learned how to cast my list as my object after i found this: Convert List to object[]
Here's what finally worked:
var actionParameterList = new List<ActionParameter>();
foreach (XmlNode xmlParameter in xmlActionParameters)
{
ActionParameter actionParameter = new ActionParameter();
actionParameter.Name = xmlParameter["Name"].InnerText;
actionParameter.Value = xmlParameter["Value"].InnerText;
actionParameterList.Add(new ActionParameter { Name = actionParameter.Name, Value = actionParameter.Value });
}
NewActionConfiguration newAction = new NewActionConfiguration
{
Name = xmlPrimaryAction["Name"].InnerText,
TemplateToken = xmlPrimaryAction["ActionTemplate"].InnerText,
Parameters = new ActionParameters
{
Parameter = actionParameterList.Cast<ActionParameter>().ToArray()
}
};

ODataComplexValue Actions and Casting

So I'm new to OData and I'm using Simple.OData.Client
So my code first looked like this :
var context = new ODataClient("http://localhost:51861/API/");
var test = context.For<Account>()
.Key("00010017")
.Action("Testing")
.Set(new Entry() { { "MyTest", New Test() { Item = "Hello World"} } })
.ExecuteAsScalarAsync<Test>()
.Result;
Leads to the following error:
"The value for parameter 'MyTest' is of type 'ODataExample.Model.MyTest'. WriteValue can only write null, ODataComplexValue, ODataEnumValue and primitive types that are not Stream type."
Ok, cool, so the exception above is informative and I can work with this by doing
ODataComplexValue MyTest = new ODataComplexValue();
ODataProperty myP = new ODataProperty();
myP.Name = "Item";
myP.Value = "Hello World";
myCustomer.TypeName = "ODataExample.Model.MyTest";
myCustomer.Properties = new[] { myP };
So now if I pass myTest into the .Set I get a working call to my OData server
.Set(new Entry() { { "MyTest", MyTest )
So Obviously I can create a ODataComplexValue with something like
var tt = new ODataComplexValue()
{
TypeName = t.GetType().FullName,
Properties = s.GetType().GetProperties().Select<PropertyInfo,ODataProperty>(x => new ODataProperty { Name = x.Name, Value = x.GetValue(t) })
};
The above works great and I an just create an extension method from that to have
.Set(new Entry() { { "MyTest", New Test() { Item = "Hello World"} } }.ToOData())
However, I can't help but feel I'm missing something, if the solution is this simple, why is Simple.OData.Client not just doing this for me? Is is there already an extension method or utility I should be using to get the same above result.
Just incase its required my webapi routing is as followed :
var function = builder.EntityType<Account>().Action("Testing");
function.Namespace = "Transaction";
function.Parameter<Test>("MyTest");
function.ReturnsFromEntitySet<Test>("Test");

Refactor to smaller function, how?

I have a function that loads a large selectlist for ASP.NET MVC.
This functions has a methodsize of 354 rows.
I want to refactor to more functions or to a local field so that each function will be less than 40 lines.
Here is the code snippet:
public static SelectList CreateShutterSpeedList()
{
var shutterSpeedList = new List<CameraSettingItem>();
var secNotationPostfix = "\"";
shutterSpeedList.Add(new CameraSettingItem
{
Id = ShutterSpeedDefaultValue,
Description = string.Empty
});
shutterSpeedList.Add(new CameraSettingItem
{
Id = 1,
Description = "30" + secNotationPostfix
});
etc
Maybe a private list as a variable ? Or loading from file ? Or else...?
If IDs above ShutterSpeedDefaultValue are assigned sequentially, you could create an array of descriptions first, and then convert it to CameraSettingItem list with LINQ:
var descriptions = new[] {
string.Empty
, "30" + secNotationPostfix
, ...
};
shutterSpeedList = descriptions
.Select((d,i) => new CameraSettingItem {
Id = i==0 ? ShutterSpeedDefaultValue : i
, Description = d
})
.ToList();
You could also create a list of CameraSettingItems outside of your method's body, like this:
private const string secNotationPostfix = "\"";
private static IList<CameraSettingItem> shutterSpeedList = new List<CameraSettingItem> {
new CameraSettingItem {
Id = ShutterSpeedDefaultValue,
Description = string.Empty
},
new CameraSettingItem {
Id = 1,
Description = "30" + secNotationPostfix
},
...
};
public static SelectList CreateShutterSpeedList() {
return new SelectList(shutterSpeedList, "Id", "Description");
}
You can store items that you need in JSON or XML files and deserialize them when you need using JavaScriptSerializer or Json.NET for example:
public static SelectList CreateShutterSpeedList(
{
var json = File.ReadAllText(#"\ShutterSpeedList.json");
var shutterSpeedList = JsonConvert.DeserializeObject<List<CameraSettingItem>>(json);
// Convert shutterSpeedList to SelectList and return
}
Alternatively you can reduce number of lines by using collection initializer (as #dasblinkenlight pointed out) and constructors with optional parameters/object initializers if you have access to CameraSettingItem code:
public static SelectList CreateShutterSpeedList()
{
var secNotationPostfix = "\"";
var shutterSpeedList = new List<CameraSettingItem>
{
new CameraSettingItem(id: ShutterSpeedDefaultValue),
new CameraSettingItem(id: 1, description: "30" + secNotationPostfix),
...
};
// Convert shutterSpeedList to SelectList and return
}

Nest aggregation not working correctly

I have a use case where I need to do aggregation on multiple columns using C#.
I am using NEST libraries for this and I am facing the following issue
Query C# :
var searchRequest = new SearchRequest
{
SearchType = SearchType.Count,
Filter = filter,
Aggregations = new Dictionary<string, IAggregationContainer>
{
{ "a", new AggregationContainer
{
ExtendedStats = new ExtendedStatsAggregator()
{
Field = "a"
}
}
},
{ "b", new AggregationContainer
{
ExtendedStats = new ExtendedStatsAggregator()
{
Field = "b"
}
}
}
}
};
When I receive response from NEST, however I am getting only result for one aggregation. I am looking at SearchResult.Agg dictionary but it has only one entry for one aggregation field instead of two.
Let me know if I am missing soemthing or is it some issue with NEST libraries
if you are using term aggregation then you need to use aggregation with filter.
var qryRes1 = client.Search<object>(x => x
.Aggregations(ag => ag
.Filter("filter", (flt => flt
.Filter(f =>
{
FilterContainer filter = null;
filter &= f.Query(qr => qr.Term(wl => wl.OnField("a").Value("the value you need to filter for field a")));
return filter;
})
.Aggregations(agr => agr
.Terms("b", tr =>
{
TermsAggregationDescriptor<object> trmAggDescriptor = new TermsAggregationDescriptor<object>();
trmAggDescriptor.Field("b");
return trmAggDescriptor;
}))))
));
var terms = qryRes1.Aggs.Filter("filter").Terms("b");

RavenDB Collection "in" Collection query

I need to preform a query that check if a collection is in given collection, just like the regular in operation but for collections.
class Post
{
public string[] Tags {get;set;}
}
session.Queury<Post>.Where(x=>x.Tags.in(new[]{".net","c#","RavenDB"})).ToList();
so if i have in my DB:
new Post{Tags= new[]{"C#",".net"}};
it will be returned
but if i have:
new Post{Tags= new[]{"C#",".net","SQLServer"}};
it will not be returned.
Update:
what i am trying to do is this:
session.Query<Post>()
.Where(x => x.Tags.All(y => y.In(new[] { "C#", ".net", "RavenDB" })))
.ToList();
but i got System.NotSupportedException.
I manage to find a solution:
static void Main(string[] args)
{
var sessionStore = new EmbeddableDocumentStore
{
RunInMemory = true,
UseEmbeddedHttpServer = true,
Conventions =
{
DefaultQueryingConsistency = ConsistencyOptions.AlwaysWaitForNonStaleResultsAsOfLastWrite
}
};
sessionStore.Initialize();
using (var session = sessionStore.OpenSession())
{
var allTags = new[] {"C#", ".net", "RavenDB", "Linux", "Mac"};
var tagsCollection = new[] {"C#", ".net", "RavenDB"};
var complementTagsCollection = allTags.Except(tagsCollection).ToList();
session.Store(new Post
{
Tags = new List<string>{"C#",".net"}
});
session.SaveChanges();
// Posts where all their tags are in tagsCollection
var result = session.Query<Post>().Where(x => !x.Tags.In(complementTagsCollection)).ToList();
}
}
The way IN works, it matches ANY of them.
If you want to match all you have to do a separate check for each.

Categories