Find index in list of custom objects - c#

I have a list: List<myObject> myList. It currently holds 3 myObjects. Given an int, I need to know how to find the index of any one of those objects so that I can use it elsewhere. I have tried using lambda statements but for some reason they absolutely do not want to work. I'm actually unsure if they're appropriate for this scenario. I have tried using myList's IndexOf, but I can't seem to get that to work, probably because I'm using custom objects.
If anybody can tell me how to accomplish this I would really appreciate it.

how about
var myobject = myList.ToArray()[myInt];
or just
var myobject = myList[myInt];

Try simply this
var myobject = myList[value];

If you can give us some code that would help.
Without any more info, here is something you can try.
int indexFound = Array.IndexOf<myObject>(myList.ToArray(), objectToLocateIdexOf);

Related

Get Property Value From Nested Object List

I am not sure how to even title or ask this question, so apologies for any confusion.
I need to get the value of the Id from the first list in Model.Content.GetPropertyValue("SlidePanel"). I've tried many, many, many thing with my troubles usually being that "You can't do this because it's an Object". In the image below, I want to get the Id Value of "1092" as a String.
-----EDIT-------
I was able to get the value with the code below. Casted the list, grabbed the first list since it would always be that option (I wrapped it in an if, but removed it in this example), then I was able to specify the property I needed and converted as needed.
If I sound like I don't speak this language fluently, it's because I'm still fresh to development. Thanks for everyone who helped.
dynamic slidePanelObject = Model.Content.GetProperty("SlidePanel").Value;
List<object> slidePanelCast = ((IEnumerable<object>)slidePanelObject).Cast<object>().ToList();
dynamic slidePanelFirst = slidePanelCast.First();
var slidePanelId = slidePanelFirst.Id;
string slidePanelString = slidePanelId.ToString();
Model.Content.GetPropertyValue is probably returning an System.Object, so you'll need to cast your temp var to a List<T> type of some kind before you can access it like a list.
Without knowing all the types involved, here's some code that you could modify:
var temp = Model.Content.GetPropertyValue("SlidePanel") as List<TYourType>;

Using C# Linq to query nested objects

I have a PowerShell object and I want to filter the results.
public Collection<PSObject> Groups;
I have successfully achieved this using linq if I need to filter based on a direct member. But what if I want to filter based on an object 1 or 2 levels deep?
Here is the linq query to filter on a direct member:
var groupMembershipFilter = (dynamic)CsQ.Groups.Where(x => x.Members["AgentsByUri"].Value != null).ToList();
However I need to drill down another level. In PowerShell this would be easy:
$x.AgentsByUri.AbsoluteUri
I have tried all sorts but cant figure out how to make this work. So that you can better understand the object structure here are a couple of screen shots:
From the above screen shots you can see the "AgentsByUri" is a collection. Inside that collection I want to test if the property "AbsoluteUri" contains a certain string value.
The other thing I dont understand is why I have to use "Members" and cant just use "BaseObject" - this structure look far more similar to PowerShell and would be my preference if possible as it translates better to my PowerShell brain.
Excuse my terminology, I'm reasonably new to C#! Hopefully this makes sense :)
Any help or guidance would be much appreciated.
Try this:
var groupMembershipFilter = (dynamic)CsQ.Groups.Where(x => x.Members["AgentsByUri"].Any(x => x.AbsoluteUri == "url")).ToList();

Is there any way to make right to left value assigning in linq or in any other way

The title is pretty unclear. But I couldn't find the proper words. Generally Linq works in the below syntax
MyList.Where().Select(x => {MyFunction(x);})
It is good in ordinary conditions but in some situation like in my case. I am creating a tree structure using dictionary. In this if I want to add a set
Set.Foreach(x => {(MyDict[logEvent.level][logEvent.event][logEvent.subevent][logEvent.filePath]).Add(x);});
But it would be nice if I can do like below
(MyDict[logEvent.level][logEvent.event][logEvent.subevent][logEvent.filePath]).Add(MySet.Foreach(x => {return x;}));
Is there any way possible to dothis ?
You can do it, if object stored in Dict has AddRange method which accepts IEnumerable<T>. But you should ski[ ForEach and just pass MySet:
MyDict[logEvent.level][logEvent.event][logEvent.subevent][logEvent.filePath]).AddRange(MySet);

Selecting all objects from a list that return true on a member function, as a list?

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)));

How to convert the following lines to Generic LINQ statements

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).

Categories

Resources