简体   繁体   中英

How to lookup table2 using a field in table1 and retrieve a column data from table2?

I have an array of objects like below in varialble SI's. I am trying to lookup another table ods.tblSoftwareImages using SoftwareImageId and get a list of all SoftwareImage from the table ods.tblSoftwareImages . Can anyone provide guidance on how to do that?

SIs
[
  {
    "ProductLineID": 17646,
    "SoftwareImageId": 17646,
    "SoftwareProductId": "2032882"
  },
  {
    "ProductLineID": 17646,
    "SoftwareImageId": 17646,
    "SoftwareProductId": "2032881"
  },
  {
    "ProductLineID": 17645,
    "SoftwareImageId": 17645,
    "SoftwareProductId": "2032883"
  }
]

public IEnumerable<SoftwareImage> GetSIForSP(int SoftwareProductID)
{
    var SIs = _entities.tblSoftwareProductSoftwareImages
        .Where(x =>x.SoftwareProductId == SoftwareProductID).ToList();
    return null;
}

You could select imageIds from your SIs and query in images table on these Ids. Just sample code: i don't know exactly what is the name of your table and how Id property looks like.

public IEnumerable<SoftwareImage> GetSIForSP(int SoftwareProductID)
{
    var imageIds = _entities.tblSoftwareProductSoftwareImages
        .Where(x =>x.SoftwareProductId == SoftwareProductID)
        .Select(x=>x.SoftwareImageId)
        .ToList();
    var images = _entities.tblSoftwareImages
                            .Where(x=>imageIds.Contains(x.SoftwareImageId));
    return images;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM