我有一个抽象类,其中收件人可以是EMailRecipient或SMSRecipients。
我在List
List<Recipient>
中选择数据。出于统计原因,我需要根据SMSRecipient中的CountryCode知道接收者的数量,我想我可以在linq中做到这一点。但我似乎无法将收件人转为像
var q = from v in AMessageQueueContentList
group v by (FlexyNet.Data.SMSRecipient)v.Recipient into g
select count(g);
我的班级看起来像这样:
public abstract class Recipient
{
public int MemberID { get;set;}
public Recipient(){}
}
public class EMailRecipient : Recipient
{
public string EMail { get; set; }
public bool ValidMail { get; set; }
public EMailRecipient(FlexyDataReader read)
{
this.MemberID = (int)read["MemberID"];
this.EMail = (string)read["Recipient"];
}
public EMailRecipient(int memberID,string eMail)
{
this.MemberID = memberID;
this.EMail = (string)eMail;
}
}
public class SMSRecipient : Recipient
{
public string Number { get; set; }
public string CountryCode { get; set; }
public nimta.Recipient NimtaRecipient { get; set; }
}
答案 0 :(得分:4)
你可以这样做吗?
var counts = from m in AMessageQueueContentList
let s = m.Recipient as FlexyNet.Data.SMSRecipient
where s != null
group s by s.CountryCode into g
select new { CountryCode = g.Key, Count = g.Count() };
或者,如果您确实有一个Recipient对象列表,则可以使用LINQ的OfType<T>()
扩展方法:
var sms = recipients.OfType<FlexyNet.Data.SMSRecipient>();
var counts = from s in sms
group s by s.CountryCode into g
select new { CountryCode = g.Key, Count = g.Count() };