Tag: select

DataTable选择具有多个AND条件的实现

我有一个大约有50,000行的DataTable,我正在使用DataTable.Select从中检索行。 Select需要多个AND条件,包括通配符匹配。 我玩过,发现通过多步执行相同的Select ,可以大大减少执行时间,但更改AND语句的顺序不会影响它。 //This takes ~ 750 ms DataRow[] results = myDataTable.Select(“Field1 LIKE ‘*” + term1 + “*'” + “AND Field2 = ‘” + term2 + “‘” + “AND Field3 = ‘” + term3 + “‘”); //This also takes ~750 ms DataRow[] results2 = myDataTable.Select(“Field3 = ‘” + term3 + “‘” + “AND Field2 […]

如何在linq中处理空值?

recordsList.ListOfRecords = new StudentRecordsBAL() .GetStudentsList() .Select(q => new StudentRecords() { _RollNumber = q._RollNumber, _Class = q._Class, _Name = q._Name, _Address = q._Address, _City = q._City, _State = q._State, _Subjects = q._Subject, _AttendedDays = new AttendanceBAL() .GetAttendanceListOf(q._RollNumber) .Where(date => date != null) .Select(date => new DateTime(date._Date.Year, date._Date.Month, date._Date.Day)) .Distinct() .ToList(), _AttendedSubjects = GetAttendedSubjects(q._RollNumber) }).ToList(); 上述代码中的方法GetAttendanceListOf(q._RollNumber)将返回数据库中的记录列表,如果传递的“roll-no”没有记录,则返回“null”。 linq查询将终止生成错误 […]

如何为工人座位安排生成“社交高尔夫球手”矩阵?

这一挑战是社交高尔夫球手问题场景。 我有一个有320人的公司。 我最近实施了一个按目标管理(MBO)计划,其中每个工作人员都被分配了每月完成的目标。 其中一个经常性的目标是按时到达工作,每天早上参加30分钟的咖啡和dounut会议。 会议在我们的餐厅举行,共有50张桌子。 每张桌子最多可容纳8人。 每个工作日正负80个座位是空的,因为目前最大容量为400个。我想生成一个循环座位安排,以便每个人可以轮流与其他人会面和协作。 (编辑)规则:每个工作日需要8人独特。 一个人不能再与他们过去坐过的其他人坐在一起,直到所有可能的排列都已用完为止。 编辑:所需结果的一个例子是: Day 1: Table 1 will seat worker numbers 1,2,3,4,5,6,7,8. Table 2 will seat worker numbers 9.10,11,12,13,14,15,16 … Table 50 will seat worker numbers 313,314,315,316,317,318,319,320 **NOTE:** (So, the next workday and thereafter, workers 1 through 8 cannot ever be seated with any other workers from that […]

在翻译LINQ表达式时,C#编译器如何选择SelectMany?

Enumerable.SelectMany有4个重载签名。 为简单起见,我们忽略了带有int参数的两个签名。 所以我们有4个SelectMany签名: public static IEnumerable SelectMany( this IEnumerable source, Func<TSource, IEnumerable> selector ) public static IEnumerable SelectMany( this IEnumerable source, Func<TSource, IEnumerable> collectionSelector, Func resultSelector ) 我的问题是:在将LINQ表达式转换为扩展方法调用时,C#编译器如何选择SelectMany? 基本上,如果LINQ表达式中有多个from ,则会有SelectMany。 但是,似乎C#编译器只选择第二个签名。 永远不会使用第一个签名。 IEnumerable en1 = Enumerable.Range(1, 3); IEnumerable en2 = new double[] { 1.0, 3.14 }; IEnumerable en3 = from i1 in en1 from i2 […]

LINQ。 从多个表中选择

在项目中我有这个表: 产品(id,catalogId,manufacturerId ……) 目录 生产厂家 还有Product型号(id, name, catalogId, catalogTitle, manufacturerId, manufacturerName) 。 如果我想获得Product item,如何在Linq中写下这个SQL查询? SELECT Product.Name, Product.CatalogId, Product.ManufacturerId, [Catalog].Name, Manufacturer.Name FROM Product, [Catalog], Manufacturer WHERE [Catalog].Id=Product.CatalogId AND Manufacturer.id=Product.ManufacturerId AND Product.Active=1

为什么IEnumerable .Select()在2种情况中有1种工作? 无法从使用中推断出来

我收到此错误消息: The type arguments for method ‘System.Linq.Enumerable.Select(System.Collections.Generic.IEnumerable, System.Func)’ cannot be inferred from the usage. Try specifying the type arguments explicitly. 第一种方法使用IEnumerable.Select()没有问题? 第二种方法的问题在哪里? private void GetPupilsForSchoolclass() { ObservableCollection pupilsOC = new ObservableCollection ( _adminRepo.GetPupilsBySchoolclassId(_selectedSchoolclass.SchoolclassId).Select(p => new PupilViewModel(p, _adminRepo)) ); SelectedSchoolclass.PupilListViewModel = pupilsOC; } private void GetDocumentsForPupil() { ObservableCollection documentsOC = new ObservableCollection(); IEnumerable documents = _docRepo.GetDocumentsByPupilId(_selectedPupil.Id); […]

带有日期的c#datatable select语句

我正在尝试在数据表上创建一个select语句,以获取我正在寻找的日期范围内的行。 我是新手,我不太明白这个选择语句是如何工作的。 我试着写这个但是没有用。 你能帮我一把吗? 我被卡住了 foundRows = dt.Select(“DATE1 = ‘” + date1+ ‘”‘);

为什么我在此CLOB字段的GetOrdinal函数中获得OutOfRangeexception?

这是我的代码示例。 字段FUNCTION_SCRIPT是我的表IS_FUNCTION中的CLOB字段(唯一的CLOB字段) public void ReadFunction(string FName, out string fContent) { OracleCommand command = _connection.CreateCommand(); OracleTransaction transaction = _connection.BeginTransaction(); command.Transaction = transaction; command.CommandText = “SELECT TO_CLOB(TO_NCLOB(FUNCTION_SCRIPT)) FROM IS_FUNCTION where FNAME=:fName “; command.Parameters.Add(“FName”, OracleType.NVarChar).Value = FName; OracleDataReader odr = command.ExecuteReader(); int temp = odr.GetOrdinal(“FUNCTION_SCRIPT”); OracleLob myLob = odr.GetOracleLob(temp); fContent = (String)myLob.Value; odr.close(); } 当执行temp = odr.GetOrdinal(“FUNCTION_SCRIPT”)语句时,我得到一个超出范围的exception。 不明白为什么? […]

2 DataTable之间的差异

我有2个DataTable,我想创建第三个DataTable,它包含DataTable 1和DataTable 2之间的区别。 例如,DataTable1具有原始数据,而DataTable 2只是一个副本,就像复制一样。 但是当您在DataTable1中插入新行时,DataTable2只插入相同的行。 现在我的代码在DataTable1和DataTable2之间进行比较,如果不是等于(插入1行或更多行),DataTable2再次记录来自DataTable1的所有数据。 如何执行select命令,执行此差异并将这些数据记录在第三个DataTable中?

$ select和$ expand break ODataQueryOptions – 如何修复?

我们将Microsoft ASP.NET MVC OData WebAPI用于我们的Web服务。 由于围绕层次结构ID的某些数据架构问题(这些问题超出了此对话的范围),我们的一些GET操作必须使用ODataQueryOptions并手动操作表达式以添加其他限制。 我们这样做(删除error handling代码并调用内联的其他方法) public IQueryable Get(ODataQueryOptions oDataQueryOptions) { IQueryable result; IQueryable dataSet = context.Persons; var tempQuery = oDataQueryOptions.ApplyTo(dataSet).Cast(); var modifier = new HierarchyNodeExpressionVisitor(GetDescendantsOfNode, GetAncestorsOfNode); var expression = modifier.ModifyHierarchyNodeExpression(tempQuery.Expression); result = context.Persons.Provider.CreateQuery(expression); return result; } 这已经有一段时间了,但是我们一直在急切地等待选择和扩展,以便我们能够更好地控制从服务中获得的数据。 星期一我们将我们的开发环境更新为WebApi OData 5.0.0-rc1并进行了select-and-expand工作,但我们不能将它用于使用ODataQueryOptions的这些服务。 我们只能用它来对付我们的其他服务。 如果我们使用$select和/或$expand查询上面的代码,我们会收到以下错误: “message”: “The ‘ObjectContent`1’ type failed to serialize the response body […]