i'm learning the book:
使用C#4.0 LINQ到对象
好的,我的问题:
我有两个课程:联系和 CallLog 。它看起来像:
public class Contact
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public DateTime DateOfBirth { get; set; }
public string State { get; set; }
};
public class CallLog
{
public string Number { get; set; }
public int Duration { get; set; }
public bool Incoming { get; set; }
public DateTime When { get; set; }
}
关系: Contact.Phone等于CallLog.Number
两个类有方法:SampleData()。该metod返回一个简单的List<>与联系人和CallLogs。
List<Contact> contacts = Contact.SampleData();
List<CallLog> callLogs = CallLog.SampleData();
我的查询是:每次联系的电话数量。
var query2 = (from contact in contacts
join callLog in callLogs on contact.Phone equals callLog.Number into joined
from callLog in joined.Where(p=>p.Incoming == false).DefaultIfEmpty()
select new
{
who = contact.FirstName + " " + contact.LastName + " " + contact.Phone,
how_many = callLog != null ? callLogs.Where(s =>s.Number == contact.Phone).Count() : 0
}).Select(p=>p).Distinct();
foreach (var q in query2)
{
Console.WriteLine(q.who + " " + q.how_many);
}
结果:
Tom XXXX 555-555-555拨打2次
Sophie YYYY 333-333-333致电3次
Mark ZZZZ 111-111-111拨打0次
现在我想只选择callLog,其中Incoming == true:
var query2 = (from contact in contacts
join callLog in callLogs.Where(p=>p.Incomming == true) on contact.Phone equals callLog.Number into joined
from callLog in joined.Where(p=>p.Incoming == false).DefaultIfEmpty()
select new
{
who = contact.FirstName + " " + contact.LastName + " " + contact.Phone,
how_many = callLog != null ? callLogs.Where(s =>s.Number == contact.Phone).Count() : 0
}).Select(p=>p).Distinct();
foreach (var q in query2)
{
Console.WriteLine(q.who + " " + q.how_many);
}
(查看新查询2中的第二行) 但结果是一样的:
Tom XXXX 555-555-555拨打2次
Sophie YYYY 333-333-333致电3次
Mark ZZZZ 111-111-111拨打0次
我该怎么办呢?
答案 0 :(得分:1)
试试这个:
var query2 = (from contact in contacts
join callLog in callLogs on contact.Phone equals callLog.Number into joined
from callLog in joined.Where(p=>p.Incoming == true).DefaultIfEmpty()
select new
{
who = contact.FirstName + " " + contact.LastName + " " + contact.Phone,
how_many = callLog != null ? callLogs.Where(s =>s.Number == contact.Phone).Count() : 0
}).Select(p=>p).Distinct();
foreach (var q in query2)
{
Console.WriteLine(q.who + " " + q.how_many);
}
答案 1 :(得分:0)
考虑问题而不是代码,我认为以下更好地表达了您正在尝试做的事情:
var contacts = new List<Contact>
{
new Contact { FirstName = "Tom", LastName = "Jones", Phone = "555-555-555" },
new Contact { FirstName = "Sophie", LastName = "Monk", Phone = "333-333-333" },
new Contact { FirstName = "Mark", LastName = "Twain", Phone = "111-111-111" }
};
var callLogs = new List<CallLog>
{
new CallLog { Number = "555-555-555", Incoming = true },
new CallLog { Number = "555-555-555", Incoming = true },
new CallLog { Number = "333-333-333", Incoming = true },
new CallLog { Number = "333-333-333", Incoming = true },
new CallLog { Number = "333-333-333", Incoming = true }
};
var data = contacts.Select(c => new
{
who = string.Format("{0} {1} {2}", c.FirstName, c.LastName, c.Phone),
how_many = callLogs.Count(l => l.Number == c.Phone && l.Incoming)
}).ToList();
data.Dump();
}
public class Contact
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public DateTime DateOfBirth { get; set; }
public string State { get; set; }
}
public class CallLog
{
public string Number { get; set; }
public int Duration { get; set; }
public bool Incoming { get; set; }
public DateTime When { get; set; }
用Linqpad编写的示例,将其直接复制/粘贴到编辑器中,您应该看到以下输出: