Reputation: 1236
I have a list that contains 3 variables, expense, salary and year. I would like to return all expenses from the list using LINQ. Writing a foreach loop would suffice but I was hoping to archive something more efficient using LINQ.
I'm thinking something like this:
List<Company> ListCompanies = new List<Company>();
Company company1 = new Company() { Expense = 200, Salary = 1000, Year = new DateTime(2014, 1, 1).ToString("yyyy/mm/dd") };
Company company2 = new Company() { Expense = 300, Salary = 800, Year = new DateTime(2014, 2, 1).ToString("yyyy/mm/dd") };
Company company3 = new Company() { Expense = 500, Salary = 1400, Year = new DateTime(2014, 3, 1).ToString("yyyy/mm/dd") };
ListCompanies.Add(company1);
ListCompanies.Add(company2);
ListCompanies.Add(company3);
var Exp = ListCompanies.Where(e => ListCompanies.Contains(e.Expense));
This doesn't work as I'm getting this error:
Argument 1: cannot convert from 'int' to 'Project1.Company
I know I could get the direct values out of a LINQ query like this:
var CompanyName = ListCompanies.Where(cn => cn.Name.Contains("X"));
which would give me all company names that contains "X". Isn't it possible to get all company names in ListConpanies
?
Hope I'm not to unclear.
Thanks!
Upvotes: 1
Views: 1808
Reputation: 1543
You are getting the error because you are searching an int Expense in a collection of (Company type) companies so type difference causes the error.
You can select any property from a IEnumerable by using Select extenion Method and it will return IEnumerable.
var Expenses = ListCompanies.Select(c => c.Expenses);
var Salaries = ListCompanies.Select(c => c.Salary);
var Years = ListCompanies.Select(c => c.Year);
These all Expenses, Salaries and Years are of IEnurable type. If you want list from this then you have to call ToList() extension method.
var Expenses = ListCompanies.Select(c => c.Expenses).ToList();
var Salaries = ListCompanies.Select(c => c.Salary).ToList();
var Years = ListCompanies.Select(c => c.Year).ToList();
Upvotes: 1
Reputation: 16878
If you want to get all salaries, simply use Select:
var salaries = ListCompanies.Select(e => e.Salary );
Also, you are not right when saying that:
var CompanyName = ListCompanies.Where(cn => cn.Name.Contains("X"));
would give you all company names that contains "X". It will return a IEnumerable<Company>
of companies which name contains "X". It might not single but multiple companies. You can then select their salaries by combining Where
and Select
clauses:
var filteredSalaries = ListCompanies.Where(c => c.Name.Contains("X"))
.Select(c => c.Salary);
Upvotes: 1
Reputation: 726579
You can get all salaries into a separate list with a Select
:
var allSalaries = ListCompanies.Select(c => c.Salary).ToList();
This produces a list wit {1000, 800, 1400}
If you do not want a list, but wish to iterate over a particular attribute from your list, you can do a Select
in a foreach
loop:
foreach (var expense in ListCompanies.Select(c => c.Expense)) {
...
}
Upvotes: 1
Reputation: 5844
To get all salaries, try this:
var salaries = ListCompanies.Select(c => c.Salary);
This will get you an IEnumerable
. If desired, you can convert it using ToArray()
, ToList()
etc.
Upvotes: 1