3
\$\begingroup\$

Is it possible to write the two following lines of code in a more compact one?

IEnumerable<int> 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<int> partnerNumbers, DateTime startDate)
{
    ...
}

...

[Table("Conracts")]
public class Contract
{
    public virtual Contact contact
    {
        get;
        set;
    }
}

...

public class Contact
{
    public virtual Collection<PartnerNumbers> 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; }
}
\$\endgroup\$
  • \$\begingroup\$ DateTime.Today \$\endgroup\$ – CodesInChaos Mar 13 '13 at 8:37
2
\$\begingroup\$

Add this read only property to your Contact class.

public class Contact
{
    public IEnumerable<int> Pnrs
    {
        get
        {
            return from pn in PartnerNumbers select pn.Pnr;
        }
    }
 }

And then you can use it like this:

var response = this.otherClass.SomeMethod(contract.Contact.Pnrs, DateTime.Now.Date);

In case your Contact class is automatically generated then you would want to put this in another partial class. Or you may want to add this as an extension method to Contact.

| improve this answer | |
\$\endgroup\$
5
\$\begingroup\$

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:

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);
}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thank you for your answer! I want to refactor this code cause it is used not only in one place and I have asked myself - whether it is possible to use some other consruct than 2 lines of code every time I want it. Thats why I have posted my qustion here :) \$\endgroup\$ – MikroDel Mar 13 '13 at 8:32
  • \$\begingroup\$ No problem. Otherwise you should create a method and re-use that method when you need it. Check my edited answer. \$\endgroup\$ – Abbas Mar 13 '13 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.