LINQ查询集合对象 [英] LINQ querying collection objects
问题描述
我是使用LINQ的新手。基本上我有一个Employee Collection对象,其中包含许多员工。然后每个员工都有一个联系人收集,因为每个员工都可以有多个联系号码/电子邮件。两者之间的链接是ContactID。我将如何使用LINQ从Employee Collection中检索一些数据以及从Contact Collection中获取其余数据。
不确定我的解释是否对任何人都有意义,但是任何反馈都会很棒。
谢谢
I''m new to using LINQ. Basically I have a Employee Collection Object, which contains a number of employees. Each employee then has a Contact Collection as each employee could have a number of contact numbers/email. The link between the two is a ContactID. How would I go about using LINQ to retrieve some data from the Employee Collection and the rest of the data from the Contact Collection.
Not sure if how I have explains that will make sense to anybody else, but any feedback would be great.
Thanks
推荐答案
这是尝试编码你的场景。
Hi,
This is an attempt to code your scenario.
class Program
{
static void Main(string[] args)
{
IEnumerable<Employee> empCollection = null;
// populate the employee collection
// now if you want to get primary contact of each employee
var result = from e in empCollection
select new
{
EmployeeName = e.Name,
PrimaryContact = e.Contacts.Select(c => c.IsPrimaryContact).First()
};
// result will be the IEnumerable<anonymoustype>, which contains EmployeeName and the associated primary contacts
}
}
public class Employee
{
public string Name { get; set; }
public IEnumerable<Contact> Contacts { get; set; }
}
public class Contact
{
public int ContactID { get; set; }
public string EmailId { get; set; }
public string Telephone { get; set; }
public bool IsPrimaryContact { get; set; }
}
希望有所帮助:)
Hope it helps :)
这篇关于LINQ查询集合对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!