locked
LINQ basic query RRS feed

  • Question

  • Hi there,

    I am totally new to LINQ. I have a table:

    CREATE TABLE COURSE (course_no VARCHAR2(7), course_name VARCHAR2(25), credits NUMBER(2), CONSTRAINT course_course_id_pk PRIMARY KEY(course_no));

    and I want to write the following query in LINQ to list all courses that have MIS in their course number.

    Could someone please help?

    Much appreciated

    • Moved by Bob Beauchemin Thursday, May 22, 2014 1:08 AM Can't tell if this is EF or L2SQL, moving to EF forum
    Wednesday, May 21, 2014 11:41 AM

Answers

  • Change the following to check if property "No" contains "MIS":

    //const string crs = "MIS"; 
    var result = (from c in Courses
                  where c.No.Contains("MIS")
                  select c);


    Olaf Helper

    [ Blog] [ Xing] [ MVP]

    • Proposed as answer by Fred Bao Tuesday, May 27, 2014 7:03 AM
    • Marked as answer by Fred Bao Friday, May 30, 2014 5:51 AM
    Thursday, May 22, 2014 7:40 AM

All replies

  • Depends on your object model; would look like something in this way:

    var result = (from c in dbcontext.COURSE
                  where c.Contains("MIS")
                  select c);


    Olaf Helper

    [ Blog] [ Xing] [ MVP]

    Wednesday, May 21, 2014 11:52 AM
  • Thank you, I'm trying to run this as a C# Program and this is what I've got:

    class COURSE
    {
    public COURSE(string CourseNo, string CourseName, int Credits)
    {No = CourseNo;
    Name = CourseName;
    Credits = Credits;
    }
    public string No {get; set;}
    public string Name {get; set;}
    public int Credits {get; set;}
    }


    void Main()
    {
    //q. List all courses that have MIS in their course number. 
    COURSE[] Courses = { new COURSE ("MIS 101", "Intro. to Info. Systems", 3),
    new COURSE ("CS 155", "Programming in C++", 3)};

    //const string crs = "MIS";
    var result = (from c in Courses
                 where c.Contains("MIS")
                 select c);
                 
    Console.WriteLine("Courses starting with MIS are ");
    foreach (var n in result)
    {Console.Write("{0,3} {1,8} {2,5}", n.No, n.Name, n.Credits);
    Console.WriteLine();
    }

    }

    but I'm getting an error:

    'UserQuery.COURSE' does not contain a definition for 'Contains' and the best extension method overload 'System.Linq.Queryable.Contains<TSource>(System.Linq.IQueryable<TSource>, TSource)' has some invalid arguments

    Instance argument: cannot convert from 'UserQuery.COURSE' to 'System.Linq.IQueryable<string>'

    Wednesday, May 21, 2014 6:54 PM
  • Change the following to check if property "No" contains "MIS":

    //const string crs = "MIS"; 
    var result = (from c in Courses
                  where c.No.Contains("MIS")
                  select c);


    Olaf Helper

    [ Blog] [ Xing] [ MVP]

    • Proposed as answer by Fred Bao Tuesday, May 27, 2014 7:03 AM
    • Marked as answer by Fred Bao Friday, May 30, 2014 5:51 AM
    Thursday, May 22, 2014 7:40 AM
  • Actually, just realised I was trying to write C# statement and not program. 

    Still getting the following error: "} expected"

    void Main()

    {

    COURSE[] Courses = { new CourseNo ("MIS 101", "Intro. to Info. Systems", 3),
    new CourseNo ("CS 155", "Programming in C++", 3)};

    //const string crs = "MIS";
    var result = (from c in Courses
                 where c.No.Contains("MIS")
                 select c);

    Console.WriteLine("Courses starting with MIS are ");
    foreach (var n in result)
    {Console.Write("{0,3} {1,8} {2,5}", n.No, n.Name, n.Credits);
    Console.WriteLine();
    }

    }

                 
    Thursday, May 22, 2014 8:49 AM
  • Hello Amateurme,

    You have actually got what you want, and the error just shows that it lacks the “}”, please check your code carefully and add the “}”.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, May 27, 2014 7:03 AM