LINQ Filtering Select Ouput with IEnumerable <T>

I have the following methods:

Controller:

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

      

Model:

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;
}

      

And I was thinking how can I filter the result of GetApplicationList request using the result of GetReqAppMapList

I'm a bit obsessed with the fact that I need to convert / cast something to the correct type because every time I do a result. Contains (appmap.Contains to be precise), I always get the following error

Error 4 Instance argument: Cannot convert from 'System.Collections.Generic.IEnumerable<Test.Models.AppMap>'

to'System.Linq.ParallelQuery<int?>'

+3


source to share


2 answers


You have to directly join two tables in one query.

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();

      



You can remove another method if you no longer need it. No dot hangs over dead code.

0


source


There seems to be no other way to do this (as far as I know), so I need to refactor the code, I hope there will be a direct conversion and matching method in the future (too lazy), Anyway, please see below for my solution. Hope this helps someone with the same problem in the future. I'm not sure about the performance, but this should work for now.

Controller:

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

      



Model:

<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;
}

      

Side note, C # is a strongly typed language, just make sure your datatypes are the same during evaluation, since int? vs int , etc., will never compile. A small dose of LINQ is enough to send a few newbies spinning for hours. One of my skills is programming the ID-10T, but enough to remind me that my feet are still flat on the ground.

0


source







All Articles