LINQ to Entities不支持'ArrayIndex'

时间:2018-10-31 05:26:35

标签: c# entity-framework linq

LINQ to Entities不支持

'ArrayIndex'。

 var result = uow.Repository<T_Tenant_Sub_PortalSetting>().Search(x => x.StudentPortalUrl.Split('/')[0].ToString() == filterContext.HttpContext.Request.Url.Host 
            && (string.IsNullOrEmpty(x.StudentPortalUrl.Split('/')[1].ToString())) ? true : x.StudentPortalUrl.Split('/')[1].ToString() == WebConfigurationManager.AppSettings["VirtualDirectoryName"])
            .IncludeEntity(x => x.T_Tenant_Sub).AsEnumerable().FirstOrDefault();

1 个答案:

答案 0 :(得分:0)

  1. 您可以获取内存中的所有学生{id,url},然后可以对URL使用split。
  2. 根据主机和...的检查结果后,创建一个实体键类型的数组(如果id为int,则为ex:int [])。
  3. 然后您可以将数组发送到数据库以获取选定的行

//imagine this list is my entities IDbSet<...>
List<MyClass> myClasses = new List<MyClass>();

//you can read all entities {id,url} in RAM
Dictionary<int, string> keyValues = 
                new Dictionary<int, string>();


myClasses
.Where(x => true/*some conditions here*/)
.Select(x => new { x.Id, x.Url })
.ToList()//here data fetched from db
.ForEach(x => keyValues.Add(x.Id, x.Url));

//now you can use split

int[] SelectedIds = keyValues
                .Where(x => x.Value.Split('/')[0] == "host"
                        /*&& another conditions here*/)
                .Select(x=>x.Key)
                .ToArray();

//now you can pass an array to db for fetching data

//as dbset
myClasses
    .Where(x => 
        SelectedIds.Contains(x.Id));

    int[] SelectedIds = keyValues.Where(x => x.Value.Split('/')[0] == "host"/*&& another conditions here*/)
        .Select(x=>x.Key)
        .ToArray();

    //now you can pass an array to db for fetching data

    //as dbset
    myClasses.Where(x => SelectedIds.Contains(x.Id));