none
Custom linq query not created

    Question

  • 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        




    • Edited by evan chime Thursday, April 27, 2017 10:25 AM
    Wednesday, April 26, 2017 3:26 PM

All replies

  • 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

    Thursday, April 27, 2017 2:00 PM
    Moderator