none
Returning all data RRS feed

  • General discussion

  • Hi, 

    I an new to wcf so pardon me if this question is basic, I have this method in my service contract 

    [DataContract]
    public class ProductData
    {
    [DataMember]
    public string Name;
    [DataMember]
    public string ProductNumber;
    [DataMember]
    public string Color;
    [DataMember]
    public decimal ListPrice;
    }
    
    [ServiceContract]
    public interface IProductsService
    {
    [OperationContract]
    ProductData GetProduct();
    }

    I want the Get Product to return all data from the datamembers, I can get a single column by doing this

    List<string> productsList = new List<string>();
    try
    {
    
    using (AdventureWorksEntities database = new AdventureWorksEntities())
    {
    
    var products = from product in database.Products
    select product.ProductNumber;
    productsList = products.ToList();
    }
    }
    catch
    {
    // Ignore exceptions in this implementation
    }
    But I can figure out how to return all the data I want, I have tried several linq queries but to no avail. Thank you

    Monday, August 11, 2014 9:29 AM

All replies

  • List<string>productsList = new List<string>();

    The above statement is not what you should be using.

    List<ProductData> prodlist = new List<ProductData>();

    var products = (from a in database.Products
    select a).Tolist();

    productsList = products.ToList();

    //The above will get you all the properties in db.Products mapped to a Productdata.

    You can also do this

    productsList .AddRange(products.Select(obj   => new ProductData

                                                               obj.Name = Name,

                                                                obj.ProductNumber = ProductNumber));


    With the AddRange, you can selectivly select what properties you want returned from the queried.

    What the Productdata shoud be is called a DTO -- DtoProduct. The DTO should be in a project called Entities. The WCF client and service should have reference to Entities so that both know what DtoProuct is about.

    http://en.wikipedia.org/wiki/Data_transfer_object

    Monday, August 18, 2014 5:54 PM