Join two tables using linq, and fill a Dictionary of them

后端 未结 3 891
情深已故
情深已故 2021-01-06 05:17

I\'ve been searching how to join two tables (Data and DataValues, one to many) and fill a dictionary of type .

The records of Data(s) might be thousands (e.g. 500,00

3条回答
  •  耶瑟儿~
    2021-01-06 06:15

    You're querying way too many times. You can do this in one query.

    var dict = (from d in dt.Datas
                join dV in dt.DataValues on d.DataID equals dv.DataID
                where SensorIDs.Contains(dv.SensorID)
                select new { d, dV }).ToDictionary(o => o.d, o => o.dV.ToList());
    

    In your foreach loop, you are fetching all Data and for each of them, you are doing the same thing.

    Edit: Now that wasn't very clear, but I think you want to join only the DataValues that are in the SensorIDs array. In this case:

    var dict = (from d in dt.Datas
                let dV = (from dataValue in dt.DataValues
                          where SensorIDs.Contains(dataValue.SensorID) &&
                                dataValue.DataID = d.DataID
                          select dataValue)
                select new { d, dV }).ToDictionary(o => o.d, o => o.dV.ToList());
    

提交回复
热议问题