C# LINQ与查询不同

C# LINQ与查询不同,c#,sql,linq,C#,Sql,Linq,我有两个类的简单代码: 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 DateOfBi

我有两个类的简单代码:

    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; }
    }
现在在两个类中,我有了静态方法:SampleData(),在这里我从源(SQL Server-返回列表)获取所有数据

我的代码:

List<Contact> contacts = Contact.SampleData();
List<CallLog> callLogs = CallLog.SampleData();
看起来不错吧

更好的查询:

var query = from contact in contacts
                        join callLog in callLogs on contact.Phone equals callLog.Number
                        group contact by new {contact.FirstName, contact.LastName, contact.Phone} into g
                        select new
                        {
                            owner = g.Key.FirstName + " " + g.Key.LastName,
                            inComming = callLogs.Where(c=>c.Number == g.Key.Phone && c.Incoming == true).Count(),
                            outGoing = callLogs.Where(c=>c.Number == g.Key.Phone && c.Incoming == false).Count()

这将按
号码组织您的通话记录,然后为您提供该特定号码的所有传入/传出通话的列表。

显示
SampleData()的定义。
您的问题是什么?您想要输入
incomming=true
的数字吗??而
CallLog
Contact
之间的链接是什么呢?Contact.Phone=CallLog.Number是的,我想要传入=true和传入=false。SampleDate return simpleList,它返回列表和列表,但我会得到更多的所有者名称,它们存在于列表中(CallLog.Number=Contact.Phone)@cniak请参见更新,您可以使用
GroupJoin
var query = from contact in contacts
                        join callLog in callLogs on contact.Phone equals callLog.Number
                        group contact by new {contact.FirstName, contact.LastName, contact.Phone} into g
                        select new
                        {
                            owner = g.Key.FirstName + " " + g.Key.LastName,
                            inComming = callLogs.Where(c=>c.Number == g.Key.Phone && c.Incoming == true).Count(),
                            outGoing = callLogs.Where(c=>c.Number == g.Key.Phone && c.Incoming == false).Count()
var callsByContact = contacts.GroupJoin(callLogs, 
    c => c.Phone, 
    l => l.Number,
    (c, calls) => new { 
        Contact = c, 
        IncomingCalls = calls.Where(x => x.Incoming).ToList(), 
        OutgoingCalls = calls.Where(x => !x.Incoming).ToList()
    });