-3

i have a list of customer and later i query that list of customer to extract one customer data based on country code. how to achieve easily this task. here is my code snippet

public class Customer
{
        public string Name
        { get; set; }

        public double Salary
        { get; set; }

        public string CountryCode
        { get; set; }
}

private void button1_Click(object sender, EventArgs e)
{
            List<Customer> oCust = new List<Customer>();
            oCust.Add(new Customer { Name = "Tridip", Salary = 200, CountryCode = "GB" });
            oCust.Add(new Customer { Name = "Ari", Salary = 200, CountryCode = "US" });
            oCust.Add(new Customer { Name = "Dib", Salary = 200, CountryCode = "CA" });

            Customer oCustomer = oCust.Where(x => x.CountryCode == "US");
}

this line is giving error Customer oCustomer = oCust.Where(x => x.CountryCode == "US");

i could follow this below approach to solve it

    var oCustomer = oCust.Where(x => x.CountryCode == "US");

    foreach (var item in oCustomer)
    {
        Customer _Cust = new Customer();
        _Cust.Name = item.Name;
        _Cust.Salary = item.Salary;
        _Cust.CountryCode = item.CountryCode;
    }

but i like to know is there any other way around to get one customer data after querying list. please let me know with sample code. also discuss how to solve the above scenario using auto mapper with sample code.

thanks

Mou
  • 15,673
  • 43
  • 156
  • 275
  • Because Where returns IEnumerable, which might have more then 1 element. Change Where to First of Single – Uriil Jan 05 '15 at 12:51

2 Answers2

3

Use .FirstOrDefault() to get the first element matching your Where(), or null when none match:

var oCustomer = oCust.Where(x => x.CountryCode == "US").FirstOrDefault();

also discuss how to solve the above scenario using auto mapper with sample code.

You've done this under various of your questions: "Here is my question, oh and please explain this related concept to me with sample code".

This is not how Stack Overflow works. Search the web for automapper, find their Getting Started page and ask a new question if you can't figure out how to use it.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • thanks for the answer but will you show how to achieve the same using auto mapper. thanks – Mou Jan 05 '15 at 12:56
  • Please read the second part of my answer again. And you don't need automapper for this. – CodeCaster Jan 05 '15 at 12:57
  • 2
    Just for the sake of brevity, it can be reduced to var oCustomer = oCust.FirstOrDefault(x => x.CountryCode == "US"); – Matt M Jan 05 '15 at 13:03
3

The thing is that Where returns a collection of entries, but you only want one (which one?). You could solve this like

Customer oCustomer = oCust.Where(x => x.CountryCode == "US").FirstOrDefault();

but this makes no representation about which one is chosen. It just picks the first it finds or returns null.

You should really introduce a way to identify customers uniquely or operate on collections, as there may be more than once customer from the US

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139