Exception Running Query - Index was outside the bounds of the array
See original GitHub issueI’ve the following query:
public class REPORT_PlcsQuery : ICachableQueryScript
{
public class Data
{
public string Name { get; set; }
}
public object Run(object[] parameters)
{
using (var db = new VisuDb())
{
var allData = (from tag in db.Tags
where (tag.Name.StartsWith("SRM.") || tag.Name.StartsWith("CS.")) && tag.Name.IndexOf(".", 5) > 0
select new Data
{
Name = tag.Name.Substring(tag.Name.IndexOf(".") + 1, tag.Name.IndexOf(".", 5) - tag.Name.IndexOf(".") - 1)
}).ToList().DistinctBy(x => x.Name).OrderBy(x => x.Name).ToList();
return allData;
}
}
}
Exception message: Index was outside the bounds of the array
Stack trace:
at bool LinqToDB.SqlQuery.SqlFunction.Equals(ISqlExpression other, Func comparer)
at bool LinqToDB.SqlQuery.SqlBinaryExpression.Equals(ISqlExpression other, Func comparer)
at bool LinqToDB.SqlQuery.SqlBinaryExpression.System.IEquatable.Equals(?)
at ISqlExpression LinqToDB.SqlProvider.BasicSqlOptimizer.OptimizeBinaryExpression(SqlBinaryExpression be, EvaluationContext context)
at IQueryElement LinqToDB.SqlProvider.BasicSqlOptimizer.OptimizeElement(MappingSchema mappingSchema, IQueryElement element, OptimizationContext optimizationContext, bool withConversion)+(ConvertVisitor visitor, IQueryElement e) => { }
at IQueryElement LinqToDB.SqlProvider.BasicSqlOptimizer.RunOptimization(IQueryElement element, OptimizationContext optimizationContext, BasicSqlOptimizer optimizer, MappingSchema mappingSchema, bool register, Func, IQueryElement, IQueryElement> func)+(ConvertVisitor visitor, IQueryElement e) => { }
at IQueryElement LinqToDB.SqlQuery.ConvertVisitor.ConvertInternal(IQueryElement element) x 2
at T[] LinqToDB.SqlQuery.ConvertVisitor.Convert(T[] arr1, Clone clone)
at IQueryElement LinqToDB.SqlQuery.ConvertVisitor.ConvertInternal(IQueryElement element)
at T LinqToDB.SqlProvider.OptimizationContext.ConvertAll(RunOptimizationContext context, T element, Func, IQueryElement, IQueryElement> convertAction, Func, bool> parentAction)
at IQueryElement LinqToDB.SqlProvider.BasicSqlOptimizer.RunOptimization(IQueryElement element, OptimizationContext optimizationContext, BasicSqlOptimizer optimizer, MappingSchema mappingSchema, bool register, Func, IQueryElement, IQueryElement> func)
at IQueryElement LinqToDB.SqlProvider.BasicSqlOptimizer.OptimizeElement(MappingSchema mappingSchema, IQueryElement element, OptimizationContext optimizationContext, bool withConversion)
at IQueryElement LinqToDB.SqlProvider.BasicSqlOptimizer.ConvertElement(MappingSchema mappingSchema, IQueryElement element, OptimizationContext context)
at void LinqToDB.SqlProvider.BasicSqlBuilder.BuildColumns(SelectQuery selectQuery)
at void LinqToDB.SqlProvider.BasicSqlBuilder.BuildSelectQuery(SqlSelectStatement selectStatement)
at void LinqToDB.SqlProvider.BasicSqlBuilder.BuildSql(int commandNumber, SqlStatement statement, StringBuilder sb, OptimizationContext optimizationContext, int indent, bool skipAlias) x 2
at PreparedQuery LinqToDB.Data.DataConnection+QueryRunner.GetCommand(DataConnection dataConnection, IQueryContext query, IReadOnlyParameterValues parameterValues, bool forGetSqlText, int startIndent)
at ExecutionPreparedQuery LinqToDB.Data.DataConnection+QueryRunner.CreateExecutionQuery(DataConnection dataConnection, IQueryContext context, IReadOnlyParameterValues parameterValues, bool forGetSqlText)
at void LinqToDB.Data.DataConnection+QueryRunner.SetQuery(IReadOnlyParameterValues parameterValues, bool forGetSqlText)
at IDataReader LinqToDB.Data.DataConnection+QueryRunner.ExecuteReader()
at IEnumerable LinqToDB.Linq.QueryRunner.ExecuteQuery(Query query, IDataContext dataContext, Mapper mapper, Expression expression, object[] ps, object[] preambles, int queryNumber)+MoveNext()
at new System.Collections.Generic.List(IEnumerable collection)
at List System.Linq.Enumerable.ToList(IEnumerable source)
Environment details
linq2db version: current Database Server: mssql .NET Framework: net6
Issue Analytics
- State:
- Created a year ago
- Comments:9 (9 by maintainers)
Top Results From Across the Web
Index was outside the bounds of the array SQL server
It may be you are using an array and looping through it. The loop is executing more number of times than existing number...
Read more >Index outside bounds of array error - Excel
"Index was outside the bounds of the array" error when you import data from multiple tables in a SQL Server database to an...
Read more >Assign : Index was outside the bounds of the array - Help
U are accessing an empty array with some index greater than zero, it returns this error. Solution : Check the array count before...
Read more >** Troubleshooting ** "Index was outside the bounds of ...
Cause. There are several possible causes for the "Index was outside the bounds of the array" error: Scenario #1 - Client device has...
Read more >Index was outside the bounds of array
As to your error, you are selecting a single field. The returned record will contain a single field. If you try to access...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
https://stackoverflow.com/a/69100842/10646316 Just remove Take.
I have no plans to support
DistinctBy
before LINQ parsing refactoring is done. Also withlinq2db
it can be expressed by ROW_NUMBER window function.