未创建自定义linq查询 [英] Custom linq query not created

查看:46
本文介绍了未创建自定义linq查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 

使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;

类MergeTwoCSVFiles { 静态void Main() { //有关数据文件的信息,请参见编译代码"部分. string []名称= System.IO.File.ReadAllLines(@"c:\ users \ mypc \ documents \ visual studio 2017 \ Projects" + @" \ CustomJoinsLinq_cs \ CustomJoinsLinq_cs \ names.csv); string []分数= System.IO.File.ReadAllLines(@"c:\ users \ mypc \ documents \ visual studio 2017 \ Projects" + @" \ CustomJoinsLinq_cs \ CustomJoinsLinq_cs \ scores.csv); //使用命名类型合并数据源. //您可以使用var代替查询的显式类型. IEnumerable< Student> queryNamesScores = //将数据文件中的每一行拆分为一个字符串数组. 从名字到名字 令x = name.Split(',') 从分数中 让s = score.Split(',') //从两个数据文件中查找匹配的ID. 其中x [2] == s [0] //如果ID匹配,则构建一个Student对象. 选择新的Student() { 名字= x [0], 姓= x [1], ID = Convert.ToInt32(x [2]), ExamScores =(来自s.Skip(1)中的scoreAsText 选择Convert.ToInt32(scoreAsText)). ToList() }; //可选的.将新创建的学生对象存储在内存中 //以便在以后的查询中更快地访问 List< Student>学生= queryNamesScores.ToList(); foreach(学生中的学生) { Console.WriteLine("{0} {1}的平均得分为{2}.", student.FirstName,student.LastName,student.ExamScores.Average()); } /* foreach(分数中的可变项) { Console.WriteLine(item); } */ //保持控制台窗口在调试模式下打开 Console.WriteLine(按任意键退出."); Console.ReadKey(); } } 班级学生 { 公共字符串FirstName {get;放; } 公共字符串LastName {get;放; } public int ID {get;放; } 公共列表< int> ExamScores {get;放; } }

大家好

请在我运行上述程序时,仅按任意键退出.显示,这是程序的退出.它应该显示查询执行的结果,但是不显示.

在调试模式下,注意到获得了数据源(在其中一个数据源上执行了foreach(上面已注释掉),并且工作正常),但是没有创建查询,因此不会不会被执行.

scores字符串数据数组包含在scores.csv文件中的12行,尽管在调试模式下向观察窗口添加了字符串分数数组,但该数组的长度为14 .不知道这些东西有什么用吗? 与之相关.

两个类都在同一个名称空间上,在此未显示.

请帮帮忙

干杯       

解决方案

LINQ使用延迟执行.在实际触发枚举之前,不会实际运行查询.您在调试模式下提到查询正在运行,但是您注释掉的foreach与查询无关,因此我不明白为什么这很重要.

假设查询完全返回任何结果,代码本身看起来很好.由于我们没有您的CSV文件,因此很难分辨.但是,我怀疑您的查询对于您发送的数据实际上是不正确的.拆分和数组取消引用 看起来好像他们可能出问题了.我建议您从一组简单的内存数组开始,并验证您的查询是否适用于它们.然后添加文件读取,以便您确定问题出在您的数据还是查询本身.

迈克尔·泰勒
http://www.michaeltaylorp3.net


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class MergeTwoCSVFiles { static void Main() { // See section Compiling the Code for information about the data files. string[] names = System.IO.File.ReadAllLines(@"c:\users\mypc\documents\visual studio 2017\Projects" + @"\CustomJoinsLinq_cs\CustomJoinsLinq_cs\names.csv"); string[] scores = System.IO.File.ReadAllLines(@"c:\users\mypc\documents\visual studio 2017\Projects" + @"\CustomJoinsLinq_cs\CustomJoinsLinq_cs\scores.csv"); // Merge the data sources using a named type. // You could use var instead of an explicit type for the query. IEnumerable<Student> queryNamesScores = // Split each line in the data files into an array of strings. from name in names let x = name.Split(',') from score in scores let s = score.Split(',') // Look for matching IDs from the two data files. where x[2] == s[0] // If the IDs match, build a Student object. select new Student() { FirstName = x[0], LastName = x[1], ID = Convert.ToInt32(x[2]), ExamScores = (from scoreAsText in s.Skip(1) select Convert.ToInt32(scoreAsText)). ToList() }; // Optional. Store the newly created student objects in memory // for faster access in future queries List<Student> students = queryNamesScores.ToList(); foreach (var student in students) { Console.WriteLine("The average score of {0} {1} is {2}.", student.FirstName, student.LastName, student.ExamScores.Average()); } /*foreach (var item in scores) { Console.WriteLine(item); }*/ //Keep console window open in debug mode Console.WriteLine("Press any key to exit."); Console.ReadKey(); } } class Student { public string FirstName { get; set; } public string LastName { get; set; } public int ID { get; set; } public List<int> ExamScores { get; set; } }

Hi Guys

Please when I run the above program, only Press any key to exit. displays, which is the exit of the program. It is supposed to display the outcome, of the execution of the query, but it doesn't. 

On debug mode, noticed the data sources gets obtained (performed a foreach, which is commented out above, on one of the data sources, and it works fine), but the query, doesn't get created and therefore doesn't get executed.

The scores array of string data source contains 12 lines on the scores.csv file, though adding the scores array of strings  to watch window, on debug mode, says the array is of length 14. Don't know if these has anything to do with it.  

Both classes are on the same namespace, which I didn't show here.

Please can some one help   

Cheers        

解决方案

LINQ uses deferred execution. Until you actually trigger an enumeration no query is actually run. You mentioned in debug mode that the query runs but the foreach you commented out has nothing to do with the query so I don't see why that would matter.

The code itself looks fine assuming that the query returns any results at all. Since we don't have your CSV files it is hard to tell. I suspect however that your query isn't actually correct for the data you're sending it. The splits and array dereferences just look like they could go wrong. I would recommend that you start with a simple set of in-memory arrays and verify your query is working for them. Then add in the file reading so you can identify whether the issue is with your data or the query itself.

Michael Taylor
http://www.michaeltaylorp3.net


这篇关于未创建自定义linq查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆