patterncsharpMinor
Refactoring from .. in .. select to more compact one
Viewed 0 times
refactoringmoreoneselectfromcompact
Problem
Is it possible to write the two following lines of code in a more compact one?
Additional information:
IEnumerable collection = from partnerNumbers in contract.Contact.PartnerNumbers select partnerNumbers.Pnr;
var response = this.otherClass.SomeMethod(collection, DateTime.Now.Date);Additional information:
public int SomeMethod(IEnumerable partnerNumbers, DateTime startDate)
{
...
}
...
[Table("Conracts")]
public class Contract
{
public virtual Contact contact
{
get;
set;
}
}
...
public class Contact
{
public virtual Collection PartnerNumbers
{
get;
private set;
}
}
...
public class PartnerNumbers
{
public int Id { get; set; }
public virtual int Pnr { get; set; }
public string Information { get; set; }
public virtual Contact contact { get; set; }
}Solution
Idon't see why you would want to shorten that piece of code but here's the same result in one line and using a LinQ method-chain instead of query-syntax:
Edit:
Since you have no parameters that have influence on the calculation of the response, here's a method for re-usability:
or with LinQ method-chain:
var response = otherClass.SomeMethod(contract.Contact.PartnerNumbers.Select(p => p.Pnr), DateTime.Now.Date);Edit:
Since you have no parameters that have influence on the calculation of the response, here's a method for re-usability:
public int GetResponse()
{
var collection = from partnerNumbers in contract.Contact.PartnerNumbers select partnerNumbers.Pnr;
var response = this.otherClass.SomeMethod(collection, DateTime.Now.Date);
return response;
}or with LinQ method-chain:
public int GetResponse()
{
return otherClass.SomeMethod(contract.Contact.PartnerNumbers.Select(p => p.Pnr), DateTime.Now.Date);
}Code Snippets
var response = otherClass.SomeMethod(contract.Contact.PartnerNumbers.Select(p => p.Pnr), DateTime.Now.Date);public int GetResponse()
{
var collection = from partnerNumbers in contract.Contact.PartnerNumbers select partnerNumbers.Pnr;
var response = this.otherClass.SomeMethod(collection, DateTime.Now.Date);
return response;
}public int GetResponse()
{
return otherClass.SomeMethod(contract.Contact.PartnerNumbers.Select(p => p.Pnr), DateTime.Now.Date);
}Context
StackExchange Code Review Q#23825, answer score: 5
Revisions (0)
No revisions yet.