返回列表Web方法

时间:2015-12-30 00:39:59

标签: c# wcf webmethod

我有Web服务和一个方法,它通过Entity Framework向我的表'Customer'发出请求。

我想返回我的请求结果:

public List<Customer> MyMethod(int id)
{
    Model model = new Model();
    List<Customer> customer = new List<Customer>();

    try
    {
         customer = model.Customer.Where(x => x.Id == id).ToList();
    }
    catch(Exception ex)
    {
         throw new System.NullReferenceException();
    }

    return customer;

}

在调试模式下我填写了一个列表,但是当我通过SoapUI调用该方法时,我没有答案。

谢谢

4 个答案:

答案 0 :(得分:1)

我的完整代码:

Class Customer(由EF生成):

[Table("Customer")]
public partial class Customer
{
    public int Id { get; set; }

    [StringLength(50)]
    public string Name { get; set; }

    public int? Adress_id { get; set; }

    public virtual Adress Adress { get; set; }
}

班级地址(由EF生成):

[Table("Adress")]
public partial class Adress
{
    public Adress()
    {
        Customer = new HashSet<Customer>();
    }

    public int Id { get; set; }

    [Column("Adress")]
    [StringLength(255)]
    public string Adress1 { get; set; }

    [StringLength(50)]
    public string Town { get; set; }

    public virtual ICollection<Customer> Customer{ get; set; }
}

我的网络方法:

[WebMethod]
public Customer MyMethod(int id)
{
    Model model = new Model();
    Customer customer = new Customer();

    try
    {
        customer = model.Customer.Where(x => x.Name == id).First();
    }
    catch (Exception ex)
    {
        throw new System.NullReferenceException(ex.Message, ex.InnerException);
    }

    return customer;
}

答案 1 :(得分:0)

请参阅以下代码:

public List<UserInfo> GetAllUserInfoDetailes()
        {
            List<UserInfo> user = new List<UserInfo>();
            using (Model1 pubs = new Model1())
            {
                var userId = (from p in pubs.TB_UserInfo select p);
                foreach (TB_UserInfo singleUser in userId)
                {
                    UserInfo a = new UserInfo();
                    a.EUserId = singleUser.User_Id;               
                    a.EUser_Name = singleUser.User_Name;
                    a.EAlias = singleUser.Alias;
                    a.EPassword = singleUser.Password;
                    a.EState = singleUser.State;
                    a.ECurrentPrevious = singleUser.CurrentPrevious;
                    a.EEmail = singleUser.Email;
                    a.EReportDutyTime = singleUser.ReportdutyTime;
                    a.ETeam_Id = singleUser.Team_Id;
                    a.ELogin_Time = DateTime.Now;
                    user.Add(a);
                }
            }
            return user;
        }

然后它可以得到结果。

答案 2 :(得分:0)

我的Customer类中有一个虚拟属性,它对应于另一个表(外键)。如果我删除这个虚拟属性,我有一个响应,但我想返回联合

答案 3 :(得分:0)

将WebMethod属性添加到您的方法:

    [WebMethod]
    public List<Customer> MyMethod(int id)
{....}