【问题标题】:Optimizing Linq Query优化 Linq 查询
【发布时间】:2012-06-05 22:58:48
【问题描述】:

我附上了一个简单的 LINQ 查询。 我想弄清楚是否有更好更有效的方法来提取这三个字段(Field1、Field2、Field3),如下面的 LINQ 查询中所述。

from currentCons in ActiveConnections
join accounts in Accounts on currentCons.New_AccountId equals accounts.AccountId                                     
let Field1 =(from holder in MapHolders
        join entity in Entities on stringmap.ObjectTypeCode equals   entity.ObjectTypeCode
        where entity.Name == "New_Connection"
        where holder.AttributeName == "New_Data"
        where holder.AttributeValue ==  currentCons.New_DATA_Val
        select holder.Value)
 let Field2=(from holder in MapHolders
        join entity in Entities on stringmap.ObjectTypeCode equals entity.ObjectTypeCode
        where entity.Name == "New_Connection"
        where holder.AttributeName == "New_Type"
        where holder.AttributeValue == currentCons.New_Type
        select holder.Value)
let Field3=(from holder in MapHolders
        join entity in Entities on stringmap.ObjectTypeCode equals entity.ObjectTypeCode
        where entity.Name == "New_Connection"
        where holder.AttributeName == "New_EntryPoint"
        where holder.AttributeValue == currentCons.New_EntryPoint
        select holder.Value)
   where currentCons.ModifiedOn >= Convert.ToDateTime("1/1/2011")
   where currentCons.ModifiedOn <= Convert.ToDateTime("5/5/2012")
   select new 
   {
      conId = currentCons.ConnectionId,
      ConName = currentCons.name,
      TypeId = currentCons.Type,
      AccountId = currentCons.AccountId,
      OId = currentCons.OwnerId,
      BandwidthId = currentCons.Bandwidth,
      EntryPointId = currentCons.EntryPoint,
      Cap = Field1 ,
      Dir=Field2,
      EP=Field3,
      ModifiedOn = currentCons.ModifiedOn
    }

我知道另一种方法是直接放在 Select Block 中。这样做的有效方法是什么?

感谢任何指针。

谢谢。

【问题讨论】:

  • 您使用的是什么 LINQ 提供程序? LINQ到SQL? LINQ 到实体?还有什么?

标签: .net linq linq-to-sql


【解决方案1】:
var fromDate =  new DateTime(2011,1,1);
var toDatePlusOneDay = new DateTime(2012,5,5).AddDays(1); // add one day to ensure 

var result = from currentCons in ActiveConnections
            join accounts in Accounts on currentCons.New_AccountId equals accounts.AccountId                                     
            where currentCons.ModifiedOn >= fromDate
            where currentCons.ModifiedOn < toDatePlusOneDay 
            from holder in MapHolders
            where (holder.AttributeName == "New_Data" && holder.AttributeValue == currentCons.New_DATA_Val) || (holder.AttributeName == "New_Type" && holder.AttributeValue == currentCons.New_Type) || (holder.AttributeName == "New_EntryPoint" && holder.AttributeValue == currentCons.New_EntryPoint)
            select new 
            {
              conId = currentCons.ConnectionId,
              ConName = currentCons.name,
              TypeId = currentCons.Type,
              AccountId = currentCons.AccountId,
              OId = currentCons.OwnerId,
              BandwidthId = currentCons.Bandwidth,
              EntryPointId = currentCons.EntryPoint,
              ModifiedOn = currentCons.ModifiedOn,
              Cap = (holder.AttributeValue == currentCons.New_DATA_Val) ? holder.Value : null,
              Dir = (holder.AttributeValue == currentCons.New_Type) ? holder.Value : null,
              EP = (holder.AttributeValue == currentCons.New_EntryPoint) ? holder.Value : null              
            }   

我删除了以下内容,因为它没有在任何结果中使用

join entity in Entities on stringmap.ObjectTypeCode equals entity.ObjectTypeCode
        where entity.Name == "New_Connection"

另外请检查 Cap/Dir/EP 是否可以为空

【讨论】:

    猜你喜欢
    • 2019-04-15
    • 1970-01-01
    • 2021-06-14
    • 1970-01-01
    • 2022-12-13
    相关资源
    最近更新 更多