Find() and lambda-statement

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 1825 Views - Last Post: 09 June 2010 - 06:19 AM Rate Topic: -----

#1 Guest_georgich*


Reputation:

Find() and lambda-statement

Posted 08 June 2010 - 01:51 AM

Hi everybody!

I have some trouble with lambda-statement and Find() method. I have structure and collection. Here it is:
struct ORDER
        {
            public int PayersAccount;
            public int PayeesAccount;
            public double TransferAmount;
        }

ORDER myOrder;

List<ORDER> BillInfo = new List<ORDER>();

I need to find integer in collection, which is typed in textBox. The search must be in PayersAccount. I want to use lambda-statement but i don't know how to use it here.

Hope you can help me. Thanks.

Is This A Good Question/Topic? 0

Replies To: Find() and lambda-statement

#2 Charles:)   User is offline

  • D.I.C Regular

Reputation: 149
  • View blog
  • Posts: 359
  • Joined: 26-November 09

Re: Find() and lambda-statement

Posted 08 June 2010 - 04:18 AM

class Program
{
	static void Main(string[] args)
	{
		List<ORDER> BillInfo = new List<ORDER>()
		{
			new ORDER()
			{
				PayersAccount = 10, 
				PayeesAccount = 0, 
				TransferAmount = 0
			},
			new ORDER()
			{
				PayersAccount = 20, 
				PayeesAccount = 0, 
				TransferAmount = 0
			},
			new ORDER()
			{
				PayersAccount = 30, 
				PayeesAccount = 0, 
				TransferAmount = 0
			}
		};
		int search = 20;

		ORDER found = BillInfo.Find(o => o.PayersAccount == search);

		Console.WriteLine(found.PayersAccount);
		Console.Write("\nPress any key to exit...");
		Console.ReadKey();
	}
}

struct ORDER
{
	public int PayersAccount;
	public int PayeesAccount;
	public double TransferAmount;
}


The important line is
BillInfo.Find(o => o.PayersAccount == search)

This post has been edited by Charles:): 08 June 2010 - 04:19 AM

Was This Post Helpful? 0
  • +
  • -

#3 Guest_georgich*


Reputation:

Re: Find() and lambda-statement

Posted 08 June 2010 - 04:33 AM

Ok. I understand this, but how to do something like this (it doesn't work):
ORDER result = BillInfo.Find(x => x == Int32.Parse(textBox4.Text));

I need to find a value which is typed in textBox.

Thanks.
Was This Post Helpful? 0

#4 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Find() and lambda-statement

Posted 08 June 2010 - 05:49 AM

this should work.

ORDER result = BillInfo.SingleOrDefault(x => x.PayeesAccount = int.Parse(textBox4.Text));



SingleOrDefault will return a null if the criteria isn't matched by any of the objects.
Was This Post Helpful? 1
  • +
  • -

#5 Guest_georgich*


Reputation:

Re: Find() and lambda-statement

Posted 08 June 2010 - 08:38 AM

Thanks, but i do it like this:
ORDER result = BillInfo.Find(x => x.PayersAccount == int.Parse(textBox4.Text));
label6.Text = Convert.ToString(result.TransferAmount);

Was This Post Helpful? 0

#6 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Find() and lambda-statement

Posted 08 June 2010 - 08:53 AM

View Postgeorgich, on 08 June 2010 - 10:38 AM, said:

Thanks, but i do it like this:
ORDER result = BillInfo.Find(x => x.PayersAccount == int.Parse(textBox4.Text));
label6.Text = Convert.ToString(result.TransferAmount);


and if it's not found?
Was This Post Helpful? 0
  • +
  • -

#7 Guest_georgich*


Reputation:

Re: Find() and lambda-statement

Posted 08 June 2010 - 12:38 PM

I'll use an exception :sorcerer:
The main thing that it works!

This post has been edited by georgich: 08 June 2010 - 12:42 PM

Was This Post Helpful? 0

#8 Sergio Tapia   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Find() and lambda-statement

Posted 08 June 2010 - 12:41 PM

Just curious, if you don't use SingleOrDefault, do you raise an exception when nothing is found and you try to assign it to a variable?
Was This Post Helpful? 0
  • +
  • -

#9 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1660
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Find() and lambda-statement

Posted 08 June 2010 - 12:43 PM

View Postgeorgich, on 08 June 2010 - 10:38 AM, said:

It works, but i don't use SingleOrDefault :chinese:


You do realize that if what you're looking for isn't found your application is going to puke all over the user? Do you want it to just work or do you want it to work right? That's why SingleOrDefault is really the way to go for something like this.
Was This Post Helpful? 1
  • +
  • -

#10 Sergio Tapia   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Find() and lambda-statement

Posted 08 June 2010 - 12:45 PM

Then I'm guessing it raises an exception. :P
Was This Post Helpful? 1
  • +
  • -

#11 Guest_georgich*


Reputation:

Re: Find() and lambda-statement

Posted 08 June 2010 - 12:49 PM

But if itsn't found it returns 0 (Zero). :online2long:

This post has been edited by georgich: 08 June 2010 - 01:04 PM

Was This Post Helpful? 0

#12 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1660
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Find() and lambda-statement

Posted 08 June 2010 - 01:08 PM

View Postgeorgich, on 08 June 2010 - 10:38 AM, said:

The main thing that it works!


Your main goal should be for it to work right no matter what, not to just get it to work. Getting something to work when the happy path is present isn't always the right way to do something when it comes to software, getting it to work no matter what happens should be your goal.

We're just trying to give you advice on the proper way to do something, from people who have been doing this for years and years. In the end it's really up to you if you take said advice
Was This Post Helpful? 0
  • +
  • -

#13 Frinavale   User is offline

  • D.I.C Addict
  • member icon

Reputation: 205
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: Find() and lambda-statement

Posted 08 June 2010 - 01:11 PM

You really should be checking if the element was found before you try using it. I realize that you can catch Null Reference exception easily enough but why bother with the overhead of causing the exception in the first place?

A simple if statement to check if the result is null will fix everything:
int accountNumber;

if(int.TryParse(textBox4.Text, accountNumber)){
  ORDER result = BillInfo.Find(x => x.PayersAccount == accountNumber);
  if(result != null){
    label6.Text = Convert.ToString(result.TransferAmount);
  }//else the result could not be found....
}//else the account number was not a number...



-Frinny
Was This Post Helpful? 0
  • +
  • -

#14 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Find() and lambda-statement

Posted 08 June 2010 - 01:41 PM

View Postgeorgich, on 08 June 2010 - 02:49 PM, said:

But if itsn't found it returns 0 (Zero). :online2long:


In .Net 4, Find() returns null if nothing is found.
Was This Post Helpful? 0
  • +
  • -

#15 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Find() and lambda-statement

Posted 08 June 2010 - 01:45 PM

I thought it returned an empty IEnumerable.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2