简体   繁体   English

高效查询多张表c# linq

[英]Query multiple tables c# linq efficiently

I have a database with about 20 table, all of them have some columns which are same, eg, name, cost, year manufactured etc. I need to query those tables.我有一个包含大约 20 个表的数据库,它们都有一些相同的列,例如名称、成本、制造年份等。我需要查询这些表。 what is best, most efficient way.什么是最好的,最有效的方法。 tables and data have to stay as they are.表格和数据必须保持原样。

Here is what I am doing right now.这就是我现在正在做的事情。

var table1 = (from a in _entities.ta1
              join b in _entities.subTa on a.tid equals b.Id
              select new
                     {
                         id = a.id,
                         name = a.name,
                         type = a.type,
                     }).ToList();
var table2 = (from a in _entities.ta2
              join b in _entities.subTa2 on a.tid equals b.Id
              select new
                     {
                         id = a.id,
                         name = a.name,
                         type = a.type,
                     }).ToList();

var abc = table1;
abc.AddRange(table2)

var list = new List<MyClass>();

foreach(var item in abc)
{
      var classItem = new MyClass();
      classItem.id = item.id;
      classItem.name = item.name;
      classItem.type = item.type;

      list.Add(classItem);
}

return list;

This needs to be done to many table, which is not very efficient coding.这需要对许多表进行,这不是很有效的编码。

How can I improve this code?如何改进此代码?

You could use that, assuming that data types for all columns do match.您可以使用它,假设所有列的数据类型都匹配。

List<MyClass> list =
    ( //table1
     from a in _entities.ta1
     join b in _entities.subTa
     on a.tid equals b.Id
     select new MyClass()
     {
         id = a.id,
         name = a.name,
         type = a.type
     })
    .Concat(( //table2 (use .Union instead of .Concat if you wish to eliminate duplicate rows)
     from a in _entities.ta2
     join b in _entities.subTa2
     on a.tid equals b.Id
     select new MyClass()
     {
         id = a.id,
         name = a.name,
         type = a.type
     }
    )).ToList();
return list;

The reason why this could be more efficient is that it这可能更有效的原因是它

  1. groups all queries into one database query, thus causes less database traffic,将所有查询分组到一个数据库查询中,从而减少数据库流量,
  2. lets all the computation happen on the database server, therefore eliminating duplicate computation in the OP code, and让所有计算都发生在数据库服务器上,因此消除了 OP 代码中的重复计算,并且
  3. optimizes memory usage as it does NOT create a list of anonymous objects and then adding them to a new List<MyClass> .优化 memory 的使用,因为它不会创建匿名对象列表,然后将它们添加到新的List<MyClass>

This is the answer i was looking or.这是我正在寻找的答案或。 this is not ideal but it got the job done这并不理想,但它完成了工作

 var tables= new Dictionary<string, string>();
 tables.Add("table1", "subTable1");
 tables.Add("table2", "subTable2");
 foreach (var table in tables)
            {
                var tableName = table.Key;
                var subName= table.Value;
                var data = _entities.Database.SqlQuery<MyClass>($@"select 
                a.Id,a.Name,b.subName from {tableName} a  left join {subName} b on 
                a.subId=b.Id").ToList();
            }

Thank you everyone for your contribution谢谢大家的贡献

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

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