【问题标题】:C# Anonymous Array of Anonymous Objects from loopC# 来自循环的匿名对象的匿名数组
【发布时间】:2012-04-18 12:58:26
【问题描述】:

我正在调用一个基于电子邮件、名字和姓氏的网络服务进行重复检查。我从业务层返回的对象非常大,并且比我需要传回的数据多得多。在我的网络服务功能中,我只想通过 JSON 传回 10 个字段。我没有用这 10 个字段创建一个新类,而是希望循环遍历我的大型返回对象,然后只创建一个包含这 10 个字段的匿名对象列表或数组。

我知道我可以像这样手动创建一个匿名对象的匿名数组

obj.DataSource = new[]
{
    new {  Text = "Silverlight",  Count = 10,  Link = "/Tags/Silverlight"  },
    new {  Text = "IIS 7",        Count = 11,  Link = "http://iis.net"     }, 
    new {  Text = "IE 8",         Count = 12,  Link = "/Tags/IE8"          }, 
    new {  Text = "C#",           Count = 13,  Link = "/Tags/C#"           },
    new {  Text = "Azure",        Count = 13,  Link = "?Tag=Azure"         } 
};

我的问题是,我想做的正是那件事,除了循环遍历我的大对象并只提取我需要返回的字段。

private class DupeReturn
{
    public string FirstName;
    public string LastName;
    public string Phone;
    public string Owner;
    public string Address;
    public string City;
    public string State;
    public string Zip;
    public string LastModified;
}

[WebMethod]
[System.Web.Script.Services.ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string CheckForDupes(string Email, string FirstName, string LastName)
{
    contact[] list = Services.Contact.GetDupes(Email, FirstName, LastName);
    if (list != null && list.Length > 0)
    {
        List<DupeReturn> dupes = new List<DupeReturn> { };
        foreach (contact i in list)
        {
            DupeReturn currentObj = new DupeReturn
            {
                FirstName = i.firstname,
                LastName = i.lastname,
                Phone = i.telephone1,
                Owner = i.ownerid.ToString(),
                Address = i.address1_line1,
                City = i.address1_city,
                State = i.address1_stateorprovince,
                Zip = i.address1_postalcode,
                LastModified = i.ctca_lastactivityon.ToString()
            };
            dupes.Add(currentObj);
        }
        return Newtonsoft.Json.JsonConvert.SerializeObject(dupes);    
    }
}

如果我不需要的话,我真的不想增加额外的私人课程。任何帮助将不胜感激。

【问题讨论】:

  • 你有什么反对再上一堂课的??
  • 这不是我的应用程序,我只想使用一次性类,因为它只用于这个功能,没有其他用途。

标签: c# anonymous-types


【解决方案1】:

使用 LINQ,您可以创建一个匿名类型列表。

var dupes = list.Select(i => new { FirstName = i.firstname,
                                   LastName = i.lastname,
                                   Phone = i.telephone1,
                                   Owner = i.ownerid.ToString(),
                                   Address = i.address1_line1,
                                   City = i.address1_city,
                                   State = i.address1_stateorprovince,
                                   Zip = i.address1_postalcode,
                                   LastModified = i.ctca_lastactivityon.ToString()
                                    });

【讨论】:

  • 如果你使用Linq,你可以使用Take()和Skip()函数进行分页。
  • @LajosArpad - 是的,虽然我不明白这与问题有什么关系。
  • 我不认为这符合我的要求。我想知道如何从循环内部创建一个匿名列表。所以我想创建一个空列表,然后从循环内部将对象添加到该列表中。
猜你喜欢
  • 2011-01-03
  • 1970-01-01
  • 2019-02-18
  • 2010-12-25
  • 1970-01-01
  • 2011-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多