I am trying to hydrate an array using the Linq.Expression namespace as this will be built into an IQueryable projection helper.
So far, I have managed to get the constructor for the list, identify its type, and now I am stuck with how to build an expression that will iterate over the source list and and return a new item for this new array to be initialised with.
Expression.Loop seems to be my best bet, but I can't figure out how to make it loop over the source array and give me back my converted items?
Anyone got any pointers of where I can start?
Si
Related
I'm trying to use the System.Management.Automation namespace to use the built-in gridviewer from PowerShell in c#, however I can't seem to be able to get it to display my list of items, it always just returns it as one line as if I was trying to get the info of the array instead of it's members.
SomeClass[] list; //let's say I have a simple array of one object type
PowerShell posh = PowerShell.Create();
posh.AddCommand("Out-Gridview");
posh.AddArgument(list); //this inputs the list as first argument for the command
posh.Invoke(); //this now just opens a gridview with information about the list itself (length, syncroot, ...)
So what I would like is a way to translate my already existing list into a powershell-compatible object inside my C# code, preferably using only the automation library and other default .NET classes.
I know templates are common within c/c++ however I am currently trying to translate an old VB code our company uses up to a c# equivalent which brings me to my problem....
VB is an type unsafe language so we come across things like
Public Elements As New Collection
so given the line above I need to translate that to a List.. Given that a Collection can be anything from a List to a map I need to template this as efficiently as possible. I was thinking of for first draft ignoring the map option entirely and just making it a List however from my understanding Templates don't truly exist within C#... The below code is what I came up with so far and while it compiles (I have not gotten to a testing point in the rest of the code yet) I don't know if it would actually work...
Public List<ValueType> Elements = new List<ValueType>();
can anyone offer input on if this would work as a generic list where type is determined at input or if I need to change this so the constructor would look more like
Public List<ValueType> Elements = new List<typeof(foo)>();
if the above is confusing I am sorry it is for me as well, and I will try and clarify as questions come in.
this question is no longer relevant, I was able to go into the source of the calling code and determine what variable types that the lists need to support.
I'm new to LINQ and trying to get a hold of it.
It's been useful so far for various things such as cutting down the code required, like when using .ForEach() to run a function on every object.
Now I'm trying to get a list of all objects from a master list, when their IsMouseOver() function returns true.
As a standard foreach it looks like this:
this.m_EntHovered.Clear();
foreach (EntEditor ent in this.m_EntAll)
{
if (ent.IsMouseOver(mousePos))
this.m_EntHovered.Add(ent);
}
But I wanted to shortern this using LINQ, however the shortest I could get it wasn't much shorter:
this.m_EntHovered = (from ent in this.m_EntAll
where ent.IsMouseOver(input)
select ent).ToList<EntEditor>();
Is there a better way to achieve what I'm after or is what I'm doing fine?
There isn't necessarily a better way to do it, but you can write it more succinctly via:
this.m_EntHovered = this.m_EntAll.Where(ent => ent.IsMouseOver(input)).ToList();
Note that this is not the same as your original, however, as you're assigning a new list, instead of adding items to the existing list. To get the same behavior (which may not be needed), you could do:
this.m_EntHovered.Clear();
this.m_EntHovered.AddRange(this.m_EntAll.Where(ent => ent.IsMouseOver(input)));
I have a list of Object's, each Object has a Children property which is a list of Object's, which in turn has a Children property which is a list of Object's ... the same thing 4 layers deep. I'm trying to pull out every instance of Object inside the entire hierarchy. At each level down the hierarchy it's still the same type of Object.
My attempts have involved using LINQ but so far no dice.
Does anyone have another angle to approach this?
If I am following your line of logic correctly I think what you are looking for is something like what this guy wanted ...
LINQ: How to convert the nested hierarchical object to flatten object
Sounds like you are trying to flatten the object which you can do through a recursive function.
I have the following lines of code in C#
that gets data using DataTables
This is pretty generic and helps me with multiple tables.
object obj = ((DataRowView)editingElement.DataContext).Row[this.SelectedValuePath];
I want to change this statement to a generic LINQ statement so that i can use it with multiple LINQ tables too.
Can somebody help me figure this out?
I don't think you can use LINQ to make the code you wrote nicer or more elegant in any way. I assume that the type of editingElement.DataContext is object, so you'll need to write the cast anyway. If you forget about the casting, your code is just indexed access:
var rows = (DataRowView)editingElement.DataContext;
object obj = rows.Row[this.SelectedValuePath];
LINQ doesn't have any features that would make indexing nicer, so I think this is the best you can get. One possible ugly thing is that you get object as the result and you'll need to cast that to some other type (e.g. CustomerInfo).
If you were using LINQ from the beginning (to populate the data for the DataContext), you could probably write something like this to access the customer:
var rows = (IEnumerable<CustomerInfo>)editingElement.DataContext;
CustomerInfo info = rows.Row[this.SelectedValuePath];
This would be a bit more elegant, because you'd need just a single cast. However, I think that your code is fine and LINQ cannot help you (in this piece of code).