|
|
Hi guys,
Probably a simple task but i'm struggling with it.
I have a list of objects, lets call this a list of Object1. Each Object1 contains a list of another object lets say Object2.
Object2 has 2 properties. 'Name' and 'Value'.
I want to be able to select Object1 where Object2.Name equals a specified value using lambda.
eg
class Object2
{
public string Name {get; set;}
public string Value {get; set;}
}
class Object1
{
public List<Object2> theList = new List<Object2>(){ new Object2(){Name = "Blah", Value = "Blah"}};
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var myList = Object1.Where(m => m.theList.Name == "Blah")
}
}
I hope I explained myself clearly.
Many thanks for your help
Br
Nigel
|
|
|
|
|
There are a lot of things wrong with that!
Object1 is a class, not a collection, so you can;t use Where on it.
Name is not a property of the list, it;'s a property of teh objects teh list contains.
Try this:
List<Object1> myList = new List<Object1>();
myList.Add(new Object1());
myList.Add(new Object1());
myList.Add(new Object1());
IEnumerable<Object1> myResults = myList.Where(m => m.theList.FirstOrDefault(o => o.Name == "Blah") != null);
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Thanks Member 12616185,
Yes, Object1 was meant to be a collection of Object1's. My bad for not proof reading. Thanks for spotting and correcting.
Your reply worked a treat. Here's the implementation I used which served my purpose.
var myResults = Object1.Where(m => m.Object2.Select(o => o.Value == "802.11g") != null);
Many thanks again for the help
Br
Nigel
|
|
|
|
|
Member 12616185 wrote:
var myResults = Object1.Where(m => m.Object2.Select(o => o.Value == "802.11g") != null);
That's not going to do what you want. It's just going to return every item from the input sequence, since m.Object2.Select(...) returns an IEnumerable<T> which is never equal to null .
Try:
var myResults = Object1.Where(m => m.Object2.Any(o => o.Value == "802.11g"));
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi Homer, I thought the query might return all results in the collection where value == 802.11g and Object2 wasn't equal to null. I have so much to learn
I did a test run and you were right! all objects came back in the collection.
I have since implemented your recommendation and that worked
Here's my test code for ref.
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
List<Object1> origCollection = new List<Object1>()
{
new Object1() {ID = 1, theList = new List<Object2>()
{
new Object2() { Name = "Mode", Value = "802.11b" },
new Object2() { Name = "BW", Value = "20" },
new Object2() { Name = "Chan", Value = "1" },
new Object2() { Name = "Tech", Value = "SISO" }
}
},
new Object1() { ID = 2, theList = new List<Object2>()
{
new Object2(){ Name = "Mode", Value = "802.11g" },
new Object2(){ Name = "BW", Value = "20" },
new Object2(){ Name = "Chan", Value = "1" },
new Object2(){ Name = "Tech", Value = "SISO" }
}
}
};
var theCollection0 = origCollection.Where(m => m.theList.Select(n => n.Value == "802.11b") != null);
var theCollection1 = origCollection.Where(m => m.theList.Any(n => n.Value == "802.11b"));
var theCollection3 = origCollection.Where(m => m.theList.Any(n => n.Value == "802.11z"));
}
};
class Object1
{
public int ID { get; set; }
public List<Object2> theList;
}
class Object2
{
public string Name { get; set; }
public string Value { get; set; }
}
}
Many thanks for spotting and fixing this! your a star
Br
Nigel
|
|
|
|
|
Probably simpler to replace the .FirstOrDefault(test) != null with .Any(test) :
IEnumerable<Object1> myResults = myList.Where(m => m.theList.Any(o => o.Name == "Blah"));
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Good idea - it's easier to read.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I'm new to programming and is currently working on Windows Forms where i'm developing a basic Pong game. I'm trying to follow a video which shows how to make a simple Pong game but although it's easy to just copy the code being written i'm not really understanding what exactly is happening since it's not explained.
I'm particulary having trouble understanding the code blocks below.
This block of code is supposed to represent points being increased by one for player 1 i think:
if (ball.Location.Y < 0)
{
pl1Score++;
ball.Location = new Point(this.Width / 2, this.Height / 2);
}
but what does
if (ball.Location.Y < 0) mean? What does the if statement represent?
Aswell as:
if (ball.Location.X > player1.Location.X && ball.Location.X + ball.Width < player1.Location.X + player1.Width && ball.Location.Y + ball.Height > player1.Location.Y)
{
bvelY *= -1;
}
bvelY is a variable representing speed of the ball btw.
And another one is
if (ball.Location.Y > this.Height)
{
pl2Score++;
ball.Location = new Point(this.Width / 2, this.Height / 2);
}
That increases points for player 2 i think. But i'm not understanding the
if (ball.Location.Y > this.Height) statement
I haven't really found any tutorials or pages that explain these things, so that's why i'm asking here. Could someone give a good description of what these things mean? Would be appreciated.
Sorry if this may come off as a stupid question, i'm new here.
|
|
|
|
|
I think the code you are asking about is when the ball hits the edges of the area. Setting the speed *=-1 will bounce the ball when it hits the edge/wall in the other Y direction (if I understand your code correctly).
|
|
|
|
|
Are you referring to the
if (ball.Location.X > player1.Location.X && ball.Location.X + ball.Width < player1.Location.X + player1.Width && ball.Location.Y + ball.Height > player1.Location.Y)
{
bvelY *= -1;
}
block? What in the code determines that the ball hits the edges of the area and what do you mean by other Y direction?
And what about
if (ball.Location.Y < 0)
{
pl1Score++;
ball.Location = new Point(this.Width / 2, this.Height / 2);
}
It's my understanding that
pl1Score++; adds points to a player but what does
if (ball.Location.Y < 0) mean?
|
|
|
|
|
I'm assuming you know the Cartesian Coordinate system[^]. Every thing will be based on it.
if (ball.Location.Y < 0)
{
pl1Score++;
ball.Location = new Point(this.Width / 2, this.Height / 2);
}
In plain English this means; If the location of the ball is less than 0 then take the following actions. Update player ones score and give the ball a new location in the middle of the screen.
Assuming your paddles are at the top and bottom of the screen this means that player one just score a point against player two.
if (ball.Location.Y > this.Height)
{
pl2Score++;
ball.Location = new Point(this.Width / 2, this.Height / 2);
}
This means exactly the same except the ball has traveled off the screen at the bottom.
if ( ball.Location.X > player1.Location.X
&& ball.Location.X + ball.Width < player1.Location.X + player1.Width
&& ball.Location.Y + ball.Height > player1.Location.Y)
{
bvelY *= -1;
}
This one is very similar in that you are making sure that the location of the ball in both the X and Y coordinate has not hit the player one paddle.
So the ball X coordinate must be greater than the player one X coordinate
AND
the ball X coordinate plus the ball width is less than the player one X plus the player one width.
(in other words does the ball line up with the paddle in the X coordinate.
AND
the ball Y coordinate plus the ball height is greater than the player one Y coordinate.
That whole if statement tells you if the ball hit the paddle then change the direction of the ball. I know it says speed but sometimes speed and direction are equivalent.
Jack of all trades, master of none, though often times better than master of one.
|
|
|
|
|
Thank you for a very clear description of what the code blocks mean, it makes more sense to me now
|
|
|
|
|
What you'e seeing in some cases is what's called "defensive programming"; e.g.
One assumes that if Y == 0, the ball is at "ground level"; and if Y = Height of container, it is "on the ceiling".
However, with "floating point calculations" (or some "logic"), you may never reach zero or height exactly; so one plays it safe by coding "(equal or) greater / less than" versus an "exact" value that might be missed.
In this case, the code goes or projects going "past" some "stopping point".
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
I see, so assuming there's one player at the top of the screen and one at the bottom, this code block
if (ball.Location.Y < 0)
{
pl1Score++;
ball.Location = new Point(this.Width / 2, this.Height / 2);
}
means the player from the bottom gets a point if the player from the bottom misses to hit the ball? Sorry if i'm being stupid here, but i'm trying to make sense of it
|
|
|
|
|
The ball "hits the floor" (the "Y coordinate" is less than 0).
We can assume that someone other than "pl1Score" missed. ("Player 2" perhaps).
The ball restarts in the middle of the screen.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
I was trying to learn Unity and followed Sashas article Attached VM Behaviours[^] but he defines the Container like so:
public class Bootstrapper : UnityBootstrapper
{
...
protected override void ConfigureContainer()
{
base.ConfigureContainer();
Container.RegisterType<ShellWindow>(new ContainerControlledLifetimeManager());
...
}
}
But when I try and register the shellwindow it tells me that the Container does not exist. I assumed they changed something so my question is if you are not supposed to use the Bootstrapper container at all? I can register types with the code:
protected override void ConfigureContainer()
{
IUnityContainer container = new UnityContainer();
container.RegisterType<ShellMainWindow>(new ContainerControlledLifetimeManager());
base.ConfigureContainer();
}
But then how do I call Resolve without declaring a property IUnityContainer?
[Edit]
I get the error message:
Quote: Could not load file or assembly 'Microsoft.Practices.Unity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=6d32ff45e0ccc69f' or one of its dependencies. The system cannot find the file specified. All I could find out was this:
c# - How to resolve "Could not load file or assembly 'Microsoft.Practices.Prism' " error? - Stack Overflow[^]
but it did not solve my problem.
[/Edit]
modified 17-Dec-17 17:02pm.
|
|
|
|
|
Post your question on the article itself - you are much more likely to get an answer there than here
=========================================================
I'm an optoholic - my glass is always half full of vodka.
=========================================================
|
|
|
|
|
Thing is that it should be a general question. I assumed lots of people had the same issue.
|
|
|
|
|
Hrmf.. Started a new project, uploaded all dependencies again, and now everything is working as it should
|
|
|
|
|
Can we use this article as Xamarin platform.can u suggest me??
|
|
|
|
|
Ask this question under that article. We have no idea, which article you are talking about.
Secondly, MVC Page list is different than Xamarin platform. Xamarin is for mobile, MVC is for web. Please be more specific while asking.
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
Here's a pretty basic question:
I have a windows forms application that has a form that uses Form1.ShowDialog(this); to open a child form. The problem is that when an exception occurs in my child form, all i get back in the output window of Visual Studio 2017 is
Exception thrown: 'System.NullReferenceException' in TreeView.exe
I've inserted breakpoints to locate the class that's throwing the exception but I don't have really anything else to go on, other than something is null.
First off, how do I throw the exception in the child form instead of returning to caller?
Maybe there's a better way to debug this?
|
|
|
|
|
Check the stack trace of the exception in the exception helper window.
If that doesn't help, see if there is an inner exception.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Thanks for the reply! I wasn't sure which window was the exception helper, as I (stupidly) have not given exception handling and debugging the priority it deserves. So this helps me better state the problem. The Exception Helper is not showing up inline, next to my code, for exceptions that occur in the child dialog. It's just throwing the exception back to the caller in the parent window. No exception helper window opens and the application continues in the running state.
I checked my settings to make sure that Exception Helper is still enabled in Options > Debugging > General > Use the new Exception Helper.
|
|
|
|