2014-07-07 66 views
0
i'm learning the book: 

LINQ到使用C#4.0LINQ的左连接并在

好对象,我的问题:

我有两个类:联系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<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); 
      } 

结果:

汤姆XXXX 555-555-555调用2次

苏菲YYYY 333-333-333调用3次

马克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); 
      } 

(看看新QUERY2第二线) 但结果是一样的:

汤姆XXXX 555-555-555调用2次

苏菲YYYY 333-333-333调用3次

Mark ZZZZ 111-111-111要求0次

我该如何解决它?

+0

看起来很相似,你刚才的问题! – DavidG

+0

[查询中的LINQ查询2](http:// stackoverflow。com/questions/24600614/linq-query2-from-query) – DavidG

+0

不!现在我正在使用leftjoin ..我学习,它的坏? – W92

回答

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); 
} 
+0

我试过了:/不是结果.. – W92

+0

你改变了p => p.Incoming == false为p => p.Incoming == true –

+0

解决了...... look:how_many = callLog!= null? callLog(...)在这里必须加入,并在第二行改变callLogLog到callLogs.Where(p => p.Incoming == true),谢谢你的帮助并对错误感谢:) – W92

0

的问题,而不是看代码,我认为下面更好的表达你想要做什么:写在Linqpad

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; } 

样品,复制/它直接粘贴到编辑器,你应该看到下面的输出:

  • 谁HOW_MANY
  • 汤姆·琼斯555-555-555 2
  • 索菲·蒙克333-333-333 3
  • 马克·吐温111-111-111 0
+0

谢谢你!我解决了我的问题(后来我可以插入我的解决方案 - 10个小时..)但它太好了:-) – W92