简体   繁体   中英

LINQ doesn't give output I expect

 var CC = new List<Company>();

            Company t1 = new Company();
            t1.Comp = "ABC";
            t1.Area = "Area1";
            t1.Link = "https://www.google.com";
            CC.Add(t1);

            Company t2 = new Project();
            t2.Comp = "DEF";
            t2.Area = "Area2";
            t2.Link = "https://www.yahoo.com";
            CC.Add(t2);

            var a = from p in CC where p.Comp == "ABC" && p.Area == "Area1"
                    select p.Link;

Console.WriteLine(a);

            Console.Read();

In the above code I expect to see the output as "www.google.com".

But what I see is .

在此处输入图片说明

Where am I going wrong ?? and how can I see "www.google.com" ?

a is returning as IEnumerable if you want to access google then you'd like to foreach on it or do FirstOrDefault()

foreach(var item in a)
{
 Console.WriteLine(a);
}

More information can be found here

As there is no way to know how many will match, your variable a is not a single match, but the list of matches. You will need to pick one, maybe the first:

Console.WriteLine(a.FirstOrDefault());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM