简体   繁体   中英

LINQ - How to get data from multiple tables

I have my Table1 that contains the following columns.

在此处输入图像描述

My Table2 contains

在此处输入图像描述

I am currently getting all the rows from Table1 that are in a date range as follows:

dbRowList = context.Table1.Where(x => x.dateTime > from && x.dateTime < to).ToList();

However, I would like to also get only the rows that, for the same idCycle in Table2 (=id of Table1), have the 'label' field with certain string content.

How to join it or query it using LINQ?

  1. Table1 join Table2 on Table1.id = Table2.idCycle .
  2. Filter for the date range for the records in Table1 and Label in Table2.
  3. Select all columns (or specify the column that is required) from Table1.
var result = (from a in context.Table1
    join b in context.Table2 on a.id equals b.idCycle 
    where (a.dateTime > from && a.dateTime < to)
    and b.label = /* Value for Label to filter */
    select a
)
.ToList();

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