简体   繁体   English

使用linq加入两个表,并填写它们的Dictionary

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

I've been searching how to join two tables (Data and DataValues, one to many) and fill a dictionary of type . 我一直在搜索如何连接两个表(Data和DataValues,一对多)并填写类型字典。

The records of Data(s) might be thousands (eg 500,000 or more) and each Data may have 10 to 20 DataValues which makes it a much heavier query, so the performance is really important here. 数据的记录可能是数千(例如500,000或更多),每个数据可能有10到20个DataValues,这使得查询更加繁重,因此性能在这里非常重要。

here is the code I've write: 这是我写的代码:

// Passed via the arguments, for example, sensorIDs would contain:
int[] sensorIDs = { 0, 1, 2, 3, 4, 5, 6, 17, 18 };
Dictionary<Data, List<DataValue>> dict = new Dictionary<Data, List<DataValue>>();

foreach (Data Data in dt.Datas)
{
    var dValues = from d in dt.Datas
                        join dV in dt.DataValues on d.DataID equals dV.DataID
                        where (SensorIDs.Contains(dV.SensorID))
                        select dV;
    dict.Add(Data, dValues.ToList<DataValue>());
}

But this approach has a significant performance issue and takes a long time to execute. 但是这种方法存在严重的性能问题,需要很长时间才能执行。 Not sure if I need to use SQL Views. 不确定我是否需要使用SQL视图。 any suggestions? 有什么建议?

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. foreach循环中,您将获取所有Data并且对于每个Data ,您正在执行相同的操作。

Edit: Now that wasn't very clear, but I think you want to join only the DataValue s that are in the SensorIDs array. 编辑:现在还不是很清楚,但我想你只想加入SensorIDs数组中的DataValue 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());

You do not need a foreach loop in this case, you can use group join to create the dictionary straight from linq which should give you better performance. 在这种情况下,您不需要foreach循环,您可以使用组连接直接从linq创建字典,这可以为您提供更好的性能。

dict=(from DataValue d in dt.DataValues
           where sensorIDs.Contains(d.SensorID)
       group d by d.DataID 
           into datavalues
       join data in dt.Datas 
           on datavalues.Key equals data.DataId
       select new { 
         Key = data, 
         Value = datavalues
       }).ToDictionary(a=>a.Key,a=>a.Value.ToList());

or you can use linq expression methods 或者你可以使用linq表达方法

dict = dt.DataValues.Where(d=>sensorIDs.Contains(d.SensorID))
            .GroupBy(a=>a.DataID)
             .Join(dt.Datas,a=>a.Key,a=>a.DataId,
                    (a,b)=>new{Key=b,Value=a.ToList()})
        .ToDictionary(a=>a.Key,a=>a.Value);

You don't need foreach loop. 你不需要foreach循环。 Try something like this in general: 通常尝试这样的事情:

var columns = dt.Columns.Cast<DataColumn>();
dt.AsEnumerable().Select(dataRow => columns.Select(column => 
                     new { Column = column.ColumnName, Value = dataRow[column] })
                 .ToDictionary(data => data.Column, data => data.Value));

Also, consider reading this: http://blogs.teamb.com/craigstuntz/2010/01/13/38525/ 另外,请考虑阅读: http//blogs.teamb.com/craigstuntz/2010/01/13/38525/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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