Linq 林克集团

Linq 林克集团,linq,Linq,我有一个抽象类,其中收件人可以是EMailRecipient,也可以是SMSRecipients 我在列表中选择数据,就像列表一样。出于统计原因,我需要根据SMSRecipient中的CountryCode知道收件人的数量,我想我可以在linq中做到这一点。但我似乎不能把一个接受者塑造成一个像我这样的短信接受者 var q = from v in AMessageQueueContentList group v by (FlexyNet.Data.SMSRecipient)v.R

我有一个抽象类,其中收件人可以是EMailRecipient,也可以是SMSRecipients

我在
列表
中选择数据,就像
列表
一样。出于统计原因,我需要根据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; }
}

你能这样做吗

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() };
或者,如果您确实有一个收件人对象列表,则可以使用LINQ的
of type()
扩展方法:

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() };
var sms=recipients.OfType();
var计数=来自sms中的s
按s.CountryCode将s分组为g
选择新{CountryCode=g.Key,Count=g.Count()};
当你说你“似乎无法施展”时,是什么阻止了你?怎么了?是分组和排序的一个示例。我希望这就是你要找的?