【问题标题】:LINQ Filtering Select Ouput with IEnumerable<T>使用 IEnumerable<T> 过滤选择输出的 LINQ
【发布时间】:2017-07-31 09:51:43
【问题描述】:

我有以下方法:

控制器:

...
var appmap = Services.GetReqAppMapList(value);
var applist = Services.GetApplicationList(docid, appid, reqid, appmap);
...

型号:

public static IEnumerable<AppMap> GetReqAppMapList(int aiRequestTypeId)
{
    try
    {
        var appmap = new List<AppMap>();
        using (var eties = new eRequestsEntities())
        {
            appmap = (from ram in eties.ReqAppMaps 
                      where ram.IsActive == 1
                      select new AppMap
                      {
                          RequestTypeId = ram.RequestTypeId
                      }).ToList();
            return appmap;
        }
    }
    catch(Exception e)
    {
        throw e;
    }           
}

public static IEnumerable<TicketApplication> GetApplicationList(int aiDocumentTypeId, int aiApplicationTypeId, int aiRequestTypeId, IEnumerable<AppMap> appmap)
{
    try
    {
        var applicationlist = new List<TicketApplication>();                    
        using (var applicationentity = new eRequestsEntities())
        {                 
            applicationlist = (from app in applicationentity.Applications
                               where 1==1   
                                <<<Some Conditions Here???>>>
== && appmap.Contains(app.ApplicationTypeId) ==
                                && app.IsActive == 1
                               select new TicketApplication
                               {
                                   ApplicationId = app.ApplicationId,
                                   Description = app.Description,
                                   DeliveryGroupId = app.DeliveryGroupId,
                                   ApplicationTypeId = app.ApplicationTypeId,
                                   DeliveryTypeId = app.DeliveryTypeId,
                                   DocumentTypeId = app.DocumentTypeId,
                                   SupportGroupId = app.SupportGroupId
                               }).OrderBy(a => a.Description).ToList();

            return applicationlist;
}

我在想如何使用 GetReqAppMapList 的结果过滤 GetApplicationList 的查询结果

我有点坚持这样一个事实,即我必须将某些东西转换/转换为正确的类型,因为每次我执行 result.Contains(准确地说是 appmap.Contains)时,我总是会收到以下错误

错误 4 实例参数:无法从 'System.Collections.Generic.IEnumerable&lt;Test.Models.AppMap&gt;''System.Linq.ParallelQuery&lt;int?&gt;'

【问题讨论】:

  • 您如何/在哪里使用 GetReqAppMapList?您将需要提供更多代码...
  • 你能举个例子说明你打电话的方式和地点appmap.Contains
  • 由于您提到 ParallelQuery 的唯一地方是在那条错误消息中,因此很难知道您做错了什么。

标签: c# linq


【解决方案1】:

您应该在一个查询中直接连接两个表。

using (var applicationentity = new eRequestsEntities())
{                 
    applicationlist = (from app in applicationentity.Applications
                       join ram in applicationentity.ReqAppMaps on app.ApplicationTypeId equals ram.RequestTypeId
                        where ram.IsActive == 1 && app.IsActive == 1
                       select new TicketApplication
                       {
                           ApplicationId = app.ApplicationId,
                           Description = app.Description,
                           DeliveryGroupId = app.DeliveryGroupId,
                           ApplicationTypeId = app.ApplicationTypeId,
                           DeliveryTypeId = app.DeliveryTypeId,
                           DocumentTypeId = app.DocumentTypeId,
                           SupportGroupId = app.SupportGroupId
                       }).OrderBy(a => a.Description).ToList();

如果不再需要,您可以删除其他方法。没有必要挂在死掉的代码上。

【讨论】:

    【解决方案2】:

    貌似没有别的办法(据我所知),所以只好重构代码,还是希望以后有直接的转换和匹配方法(太懒了) .无论如何,请参阅下面的解决方案。希望这可以帮助将来遇到同样问题的人。我不确定性能,但现在应该可以了。

    控制器:

    ...
    var appmap = Services.GetReqAppMapList(value);
    var applist = Services.GetApplicationList(docid, appid, reqid, appmap);
    ...
    

    型号:

    <Removed GetReqAppMapList>--bad idea
    
    public static IEnumerable<TicketApplication> GetApplicationList(int aiDocumentTypeId, int aiApplicationTypeId, int aiRequestTypeId)
    {
        try
        {
           //This is the magic potion...
           List<int?> appmap = new List<int?>();
           var applist = (from ram in applicationentity.ReqAppMaps
                          where ram.RequestTypeId == aiRequestTypeId
                           && ram.IsActive == 1
                          select new AppMap
                          {
                             ApplicationTypeId = ram.ApplicationTypeId
                          }).ToList();
    
          foreach (var item in applist)
          {
             appmap.Add(item.ApplicationTypeId);
          }
          //magic potion end
    
          var applicationlist = new List<TicketApplication>();                    
          using (var applicationentity = new eRequestsEntities())
          {                 
             applicationlist = (from app in applicationentity.Applications
                                where 1==1
                            ===>>>&& appmap.Contains(app.ApplicationTypeId)<<<===
                                 && app.IsActive == 1
                                   select new TicketApplication
                                   {
                                       ApplicationId = app.ApplicationId,
                                       Description = app.Description,
                                       DeliveryGroupId = app.DeliveryGroupId,
                                       ApplicationTypeId =app.ApplicationTypeId,
                                       DeliveryTypeId = app.DeliveryTypeId,
                                       DocumentTypeId = app.DocumentTypeId,
                                       SupportGroupId = app.SupportGroupId
                                   }).OrderBy(a => a.Description).ToList();
    
                return applicationlist;
    }
    

    附带说明,C# 是一种强类型语言,只需确保您的数据类型在评估期间匹配,如 int?int 等,将从不编译。少量的 LINQ 足以让一些新手绕上几个小时。我的 ID-10T 编程经验之一,但足以提醒我我的脚仍然平放在地上。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-18
      • 2012-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-05
      • 1970-01-01
      相关资源
      最近更新 更多