0
votes

I'm trying to query my ProductiveUnits table against an array of values from the AspNetUsersGroupMaps table. A AspNetUserID can be linked to many UserGroupID's on the AspNetUsersGroupMaps table. I only want the list of ProductiveUnits returned where the ProductiveUnit.UserGroupID = AspNetUsersGroupMaps.UserGroupID as following:

   public ActionResult ProductiveUnits_Read([DataSourceRequest]DataSourceRequest request)
    {
        var UserID = User.Identity.GetUserId();
        int[] selectedIds = db.AspNetUsersGroupMaps.Where(t => t.AspNetUserID == UserID).Select(x => x.UserGroupID).ToArray();
        IQueryable<ProductiveUnit> productiveunits = db.ProductiveUnits.Where(p => p.UserGroupID.Equals(selectedIds)); 
        DataSourceResult result = productiveunits.ToDataSourceResult(request, productiveUnit => new {
            ProdUnitID = productiveUnit.ProdUnitID,
            PU_Text = productiveUnit.PU_Text,
            PUGroup = productiveUnit.PUGroup,
            SerialNo = productiveUnit.SerialNo,
            ExtSysPU_ID = productiveUnit.ExtSysPU_ID,
            ProdUnitNo = productiveUnit.ProdUnitNo,
            CompanyID = productiveUnit.CompanyID,
            Criticality = productiveUnit.Criticality,
            YearModel = productiveUnit.YearModel,
            CostCentreID = productiveUnit.CostCentreID,
            CurrencyID = productiveUnit.CurrencyID,
            StartupDate = productiveUnit.StartupDate,
            UserGroupID = productiveUnit.UserGroupID,
        });

        return Json(result);
    }

I get the following error:

Cannot compare elements of type 'System.Int32[]'. Only primitive types, enumeration types and entity types are supported.'

My AspNetUsersGroupMap Model:

public partial class AspNetUsersGroupMap
{
    public int ID { get; set; }
    public string AspNetUserID { get; set; }
    public int UserGroupID { get; set; }

    public virtual UserGroup UserGroup { get; set; }
    public virtual AspNetUser AspNetUser { get; set; }
}

Please assist

1
You're trying to compare a single int to an array of intsA Friend
Yes but I do not know what the code is for an array comparison. If i change it to contains, i get the following error: 'int[]' does not contain a definition for 'Contains' and the best extension method overload 'Queryable.Contains<int?>(IQueryable<int?>, int?)' requires a receiver of type 'IQueryable<int?>'Philip

1 Answers

1
votes

Use Contains:

p => selectedIds.Contains(p.UserGroupID)

You can't compare an int[] and int for equality, but you can check if an int[] contains a particular int.