Filter values based on custom attribute in another table

har*_*ath 5 sql linq postgresql

I have two tables DefaultAttributes and CustomAttributes.

DefaultAttributeTable: 
1. Id
2. Product
4. Description

CustomAtrributeTable:
1. Id
2. DefaultAttributeMappingId(FK from DefaultAttributeTable)
3. CustomAtrributeName
4. CustomeAttributeValue
Run Code Online (Sandbox Code Playgroud)

Entries made by user:

  1. user update the values Product -> Vegetables, Description -> House Hold Item, IsOrganic(Custom Attribute created) -> True and IsDisposable(Custom Attribute created) -> True

  2. user update the values Product -> Fruits, Description -> House Hold Item, IsOrganic(Custom Attribute created) -> True and IsDisposable(Custom Attribute created) -> True

  3. user update the values Product -> Plastic, Description -> House Hold Item, IsOrganic(Custom Attribute created) -> False and and IsDisposable(Custom Attribute created) -> False

Then the values will be updated in the table will

DeafaultAtrributeTable:

在此处输入图片说明

CustomAttributeTable: 在此处输入图片说明

I want to combine the two tables and select the Id, product, IsOrganic, IsDisposable and filter the values based on the isorganic column. Also the custom attribute column name should be taken form the CustomAtrributeTable. Please suggest to me how to achieve it in SQL and Linq Query. The filtered value should be

在此处输入图片说明

Dar*_*Rob 0

你可以在 SQL 中尝试一下

select DA.Id, DA.Product, CA.CustomeAttributeValue as IsOrganic  
from DeafaultAtrributeTable as DA inner join CustomAttributeTable as CA
on DA.Id = CA.DefaultAttributeMappingId
Run Code Online (Sandbox Code Playgroud)

在LINQ中

var query =
   from DA in DeafaultAtrributeTable 
   join CA in CustomAttributeTable  on DA.ID equals CA.DefaultAttributeMappingId
   where CA.CustomeAttributeValue == true
   select new { Id = DA.Id, Product = DA.Product, IsOrganic = CA.CustomeAttributeValue };
Run Code Online (Sandbox Code Playgroud)

或 LINQ 扩展方法是

  var query = DeafaultAtrributeTable     // your starting point - table in the "from" statement
      .Join(CustomAttributeTable  , // the source table of the inner join
         DA => DA.ID,        // Select the primary key (the first part of the "on" clause in an sql "join" statement)
         CA => CA.DefaultAttributeMappingId,   // Select the foreign key (the second part of the "on" clause)
        (DA, CA) => new { Id = DA.Id, Product = DA.Product, IsOrganic = 
     CA.CustomeAttributeValue }) // selection 
    .Where(RES => RES.CA.CustomeAttributeValue == true);    // where statement
Run Code Online (Sandbox Code Playgroud)