How to join multiple tables in entity framework

asp.net-mvc c# entity-framework

Accepted Answer

You can develop a special class that has onlyProductName and IsEnabled as in this:

public class ProductEnableInfo
{
    public string ProductName { get; set; }
    public bool IsEnabled { get; set; }
}

After that, you can adopt the following strategy:

public IEnumerable<ProductEnableInfo> GetProducts()
{
    return db.Products.Select(
        x => new ProductEnableInfo
        {
            ProductName = x.ProductName,
            IsEnabled = x.ProductDetail.IsEnabled
        })
        .ToList();
}

UPDATE:

Filtering by ProductID looks like this:

public IEnumerable<ProductEnableInfo> GetProducts(int product_id)
{
    return db.Products
        .Where(x => x.ProductID == product_id)
        .Select(
        x => new ProductEnableInfo
        {
            ProductName = x.ProductName,
            IsEnabled = x.ProductDetail.IsEnabled
        })
        .ToList();
}
2
11/6/2015 9:41:22 PM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow