这个方法的返回类型是什么

本文关键字:返回类型 是什么 方法 | 更新日期: 2023-09-27 18:11:39

我得到了以下代码,它从CRM返回一组结果,然后我将其绑定到下拉列表:

        var context = new XrmServiceContext();
        var contacts1 =
            (
                from c in context.ContactSet
                join m in context.py3_membershipSet on c.ContactId equals m.py3_Member.Id
                where m.statuscode.Value == 1
                orderby c.LastName
                select new
                {
                    ContactId = c.ContactId,
                    FirstName = c.FirstName,
                    LastName = c.LastName,
                    BranchCode = c.py3_BranchArea,
                    Branch = (c.FormattedValues != null && c.FormattedValues.Contains("py3_brancharea") ? c.FormattedValues["py3_brancharea"] : "N/a"),
                    JobTitle = c.JobTitle,
                    Organisation = (c.ParentCustomerId != null ? c.ParentCustomerId.Name : "N/a"),
                    joinedAsCode = c.py3_SOLACEMemberJoinedAs,
                    JoinedAs = (c.FormattedValues != null && c.FormattedValues.Contains("py3_solacememberjoinedas") ? c.FormattedValues["py3_solacememberjoinedas"] : "N/a"),
                    Expertise = (c.py3_SOLACEMemberAreasofExpertise != null && c.py3_SOLACEMemberAreasofExpertise.Trim() != String.Empty ? c.py3_SOLACEMemberAreasofExpertise : "N/a")
                }
            );

然而,我需要把它变成一个方法,这样我就可以调用这组结果,并根据其他一些标准对返回的数据做一些LINQ。

我刚开始使用LINQ和'var'的整个概念来包含结果集,因此我失去了什么类型的方法:

        protected static **something** getContacts()
    {
        var context = new XrmServiceContext();
        var contacts1 =
            (
                from c in context.ContactSet
                join m in context.py3_membershipSet on c.ContactId equals m.py3_Member.Id
                where m.statuscode.Value == 1
                orderby c.LastName
                select new
                {
                    ContactId = c.ContactId,
                    FirstName = c.FirstName,
                    LastName = c.LastName,
                    BranchCode = c.py3_BranchArea,
                    Branch = (c.FormattedValues != null && c.FormattedValues.Contains("py3_brancharea") ? c.FormattedValues["py3_brancharea"] : "N/a"),
                    JobTitle = c.JobTitle,
                    Organisation = (c.ParentCustomerId != null ? c.ParentCustomerId.Name : "N/a"),
                    joinedAsCode = c.py3_SOLACEMemberJoinedAs,
                    JoinedAs = (c.FormattedValues != null && c.FormattedValues.Contains("py3_solacememberjoinedas") ? c.FormattedValues["py3_solacememberjoinedas"] : "N/a"),
                    Expertise = (c.py3_SOLACEMemberAreasofExpertise != null && c.py3_SOLACEMemberAreasofExpertise.Trim() != String.Empty ? c.py3_SOLACEMemberAreasofExpertise : "N/a")
                }
            );
return contacts;
    }

应该是什么类型?

这个方法的返回类型是什么

它是一个匿名类型,所以你不能指定名称。

  • IEnumerable
  • IQueryable
  • IEnumerable<dynamic>
  • IQueryable<dynamic>

然而,我建议做一个简单的POCO类来存储你的数据,而不是返回一个IQueryable<T>

public class GetContactsResult
{
    public long ContactId { get; set; }
    public string FirstName { get; set; }
    ...
}
protected static IQueryable<GetContactsResult> getContacts()
{
    ...
    var contacts =
        (from c in context.ContactSet
         ...
         select new GetContactsResult()
         {
             ...
         });
    return contacts;
}

匿名类型被特别设计为仅在创建它们的上下文中使用。虽然可以使用几种不同的技术中的一种来返回匿名类型,但无论您做什么都会导致丢失编译器验证的静态类型,并且(在大多数情况下)也会导致性能下降。

到目前为止,最简单、最有效、最简单和最不容易出错的解决方案是创建一个新的命名类型,而不是依赖于匿名类型。一旦为每个字段创建了具有属性的新简单类型,就可以选择该类型的新实例,而不是匿名实例。

匿名类型有一个方法范围。这意味着,如果在包含方法边界之外传递匿名类型,则必须将其强制转换为对象。这意味着您唯一的选择是使用对象作为返回类型。

您可以使用反射作为替代(但它会变得很难看):

static void ContainingMethod()
{
  var anondata = new
  {
    IntegerVal = 1,
    DoubleVal = 2.0D,
    DateTimeVal = DateTime.Now,
    StringVal = "some string"       
  };
  ExternalMethod(anondata);
}
static void ExternalMethod(object data)
{
  // Get the type that was passed in
  Type t = data.GetType();
  // Get a list of the properties
  PropertyInfo[] piList = t.GetProperties();
  // Loop through the properties in the list
  foreach (PropertyInfo pi in piList)
  {
    // Get the value of the property
    object o = pi.GetValue(data, null);
    // Write out the property information
    Console.WriteLine("{0} ({1}): 't{2}", pi.Name, o.GetType(), o.ToString());
  }
}

由于这是一个匿名类型,我建议您为此创建一个类,例如:

class Person {
private int age;
private String name;
private String address;
//theirs respective getters and setters
}

,那么您就可以创建一个Person类型的linq表达式