如何把一种特定的类在c# 4.0 LINQ查询对象How to cast a type to specific class in c# 4.0 after querying LINQ to Object

- 此内容更新于:2015-01-06
主题:

原文:

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

Uriil的回复:因为那里返回IEnumerable<T>,这可能有更多然后1元素。改变的第一单

(原文:Because Where returns IEnumerable<T>, which might have more then 1 element. Change Where to First of Single)

解决方案:
原文:

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.

Mou的回复:谢谢你的回答,但你会展示如何使用汽车实现相同的映射器。谢谢

(原文:thanks for the answer but will you show how to achieve the same using auto mapper. thanks)

CodeCaster的回复:请再读我的回答的第二部分。你不要# 39;t需要automapper。

(原文:Please read the second part of my answer again. And you don't need automapper for this.)

Matty M的回复:只是为了简便起见,可以减少到var oCustomer = oCust。在FirstOrDefault(x = x。CountryCode = =“US");

(原文:Just for the sake of brevity, it can be reduced to var oCustomer = oCust.FirstOrDefault(x => x.CountryCode == "US");)

解决方案:
事情是这样的,在返回条目的集合,但是我们只需要一个(?)。你可以解决这个问题 但这并没有表示对选择哪一个。只是第一个发现或返回null。 你真的应该介绍一种确定客户独特或操作的集合,是可能会有不止一次的客户来自美国
原文:

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