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 

    public class ProductData
    public string Name;
    public string ProductNumber;
    public string Color;
    public decimal ListPrice;
    public interface IProductsService
    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>();
    using (AdventureWorksEntities database = new AdventureWorksEntities())
    var products = from product in database.Products
    select product.ProductNumber;
    productsList = products.ToList();
    // 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.

    Monday, August 18, 2014 5:54 PM