We mentioned that we could declare one or more let variables in your query but you could get only one of those variables out of the query. To let you get more that one variable outside of the query, the LINQ provides an operator name new. It must follow the select keyword in the following formula: var SubListName = from ValueHolder in List select new { }; As you can see, to use the new operator, after the select keyword, type new followed by an opening and a closing curly brackets, "{" and "}" . Inside the brackets, declare any variable of your choice and assign it the value you want. The value can be any constant you want. Here is an example: private void ExerciseLoad(object sender, EventArgs e)
{
var numbers = new double[] { 1250d, 12.05d, 36d };
var number = from n
in numbers
select new
{
something = 6500
};
}
If you declare more than one variable, separate them with commas. Here are examples: private void ExerciseLoad(object sender, EventArgs e)
{
var numbers = new double[] { 1250d, 12.05d, 36d };
var number = from n
in numbers
select new
{
something = 6500, another 4.006, more 5
};
}
Remember that you can declare each variable on its own line to make the code easier to read. If you are using the values from an array, you can locate a value by its index and assign it to the desired local variable. Here are examples: var number = from n
in numbers
select new
{
Principal = numbers[0],
InterestRate = numbers[1],
Periods = numbers[2]
};
Either way, to accessing a new variable outside the query, use a foreach loop to get a contextual variable. In the body of foreach, access each new variable using the period operator applied. Here are examples: using System;
using System.Linq;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
public class Exercise : Form
{
private Label lblPrincipal;
private TextBox txtPrincipal;
private Label lblInterestRate;
private TextBox txtInterestRate;
private Label lblPercentSign;
private Label lblPeriods;
private TextBox txtPeriods;
private Label lblMonths;
private Label lblInterestPaid;
private TextBox txtInterestPaid;
private TextBox txtFutureValue;
private Label lblFutureValue;
public Exercise()
{
InitializeComponent();
}
void InitializeComponent()
{
lblPrincipal = new Label();
lblPrincipal.AutoSize = true;
lblPrincipal.Text = "Principal:";
lblPrincipal.Size = new System.Drawing.Size(50, 13);
lblPrincipal.Location = new System.Drawing.Point(12, 20);
txtPrincipal = new TextBox();
txtPrincipal.TextAlign = HorizontalAlignment.Right;
txtPrincipal.Size = new System.Drawing.Size(81, 20);
txtPrincipal.Location = new System.Drawing.Point(89, 17);
lblInterestRate = new Label();
lblInterestRate.AutoSize = true;
lblInterestRate.Text = "Interest Rate:";
lblInterestRate.Size = new System.Drawing.Size(71, 13);
lblInterestRate.Location = new System.Drawing.Point(12, 51);
txtInterestRate = new TextBox();
txtInterestRate.Size = new System.Drawing.Size(58, 20);
txtInterestRate.Location = new System.Drawing.Point(89, 48);
txtInterestRate.TextAlign = HorizontalAlignment.Right;
lblPercentSign = new Label();
lblPercentSign.Text = "%";
lblPercentSign.AutoSize = true;
lblPercentSign.Size = new System.Drawing.Size(15, 13);
lblPercentSign.Location = new System.Drawing.Point(153, 51);
lblPeriods = new Label();
lblPeriods.AutoSize = true;
lblPeriods.Text = "Periods:";
lblPeriods.Size = new System.Drawing.Size(45, 13);
lblPeriods.Location = new System.Drawing.Point(12, 81);
txtPeriods = new TextBox();
txtPeriods.TextAlign = HorizontalAlignment.Right;
txtPeriods.Size = new System.Drawing.Size(58, 20);
txtPeriods.Location = new System.Drawing.Point(89, 78);
lblMonths = new Label();
lblMonths.Text = "Months";
lblMonths.AutoSize = true;
lblMonths.Size = new System.Drawing.Size(42, 13);
lblMonths.Location = new System.Drawing.Point(153, 81);
lblInterestPaid = new Label();
lblInterestPaid.AutoSize = true;
lblInterestPaid.Text = "Interest Paid:";
lblInterestPaid.Size = new System.Drawing.Size(69, 13);
lblInterestPaid.Location = new System.Drawing.Point(12, 111);
txtInterestPaid = new TextBox();
txtInterestPaid.TextAlign = HorizontalAlignment.Right;
txtInterestPaid.Size = new System.Drawing.Size(81, 20);
txtInterestPaid.Location = new System.Drawing.Point(89, 108);
lblFutureValue = new System.Windows.Forms.Label();
lblFutureValue.AutoSize = true;
lblFutureValue.Text = "Future Value:";
lblFutureValue.Size = new System.Drawing.Size(70, 13);
lblFutureValue.Location = new System.Drawing.Point(12, 140);
txtFutureValue = new TextBox();
txtFutureValue.TextAlign = HorizontalAlignment.Right;
txtFutureValue.Size = new System.Drawing.Size(81, 20);
txtFutureValue.Location = new System.Drawing.Point(89, 137);
ClientSize = new System.Drawing.Size(204, 174);
Controls.Add(lblPrincipal);
Controls.Add(txtPrincipal);
Controls.Add(lblInterestRate);
Controls.Add(txtInterestRate);
Controls.Add(lblPercentSign);
Controls.Add(lblPeriods);
Controls.Add(txtPeriods);
Controls.Add(lblMonths);
Controls.Add(lblInterestPaid);
Controls.Add(txtInterestPaid);
Controls.Add(lblFutureValue);
Controls.Add(txtFutureValue);
MaximizeBox = false;
MinimizeBox = false;
StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
Text = "Loan Evaluation";
Load += new System.EventHandler(this.ExerciseLoad);
}
private void ExerciseLoad(object sender, EventArgs e)
{
var numbers = new double[] { 6500d, 11.65d, 48u };
var number = from n
in numbers
select new
{
Principal = numbers[0],
InterestRate = numbers[1],
Periods = numbers[2]
};
foreach (var member in number)
{
txtPrincipal.Text = member.Principal.ToString();
txtInterestRate.Text = member.InterestRate.ToString();
txtPeriods.Text = member.Periods.ToString();
}
}
[STAThread]
public static int Main()
{
System.Windows.Forms.Application.Run(new Exercise());
return 0;
}
}
Instead of a constant or a member of an array, a new variable can be assigned an expression. The expression can be made of constants, the members of an array, or a combination of constants and members of the array. Here are examples: private void ExerciseLoad(object sender, EventArgs e)
{
var numbers = new double[] { 6500d, 11.65d, 48u };
var number = from n
in numbers
select new
{
Principal = numbers[0],
InterestRate = numbers[1],
Periods = numbers[2],
Futurevalue = numbers[0] * (1 + (numbers[1] / 100 * numbers[2] / 12)),
InterestPaid = (numbers[0] * (1 + (numbers[1] / 100 * numbers[2] / 12))) - numbers[0],
};
foreach (var member in number)
{
txtPrincipal.Text = member.Principal.ToString();
txtInterestRate.Text = member.InterestRate.ToString();
txtPeriods.Text = member.Periods.ToString();
txtFutureValue.Text = member.Futurevalue.ToString("F");
txtInterestPaid.Text = member.InterestPaid.ToString("F");
}
}
This would produce: If necessary, you can include a where statement to check a condition. The where statement must be written before the select new section. Here is ab example: private void ExerciseLoad(object sender, EventArgs e)
{
var numbers = new double[] { 6500d, 11.65d, 48u };
var number = from n
in numbers
where numbers[0] > 5000
select new
{
Principal = numbers[0],
InterestRate = numbers[1],
Periods = numbers[2],
Futurevalue = numbers[0] * (1 + (numbers[1] / 100 * numbers[2] / 12)),
InterestPaid = (numbers[0] * (1 + (numbers[1] / 100 * numbers[2] / 12))) - numbers[0]
};
foreach (var member in number)
{
txtPrincipal.Text = member.Principal.ToString();
txtInterestRate.Text = member.InterestRate.ToString();
txtPeriods.Text = member.Periods.ToString();
txtFutureValue.Text = member.Futurevalue.ToString("F");
txtInterestPaid.Text = member.InterestPaid.ToString("F");
}
}
|
|
|||
|