EntityFramework 学习 一 Table-Valued Function in Entity Framework 5.0

USE [SchoolDB]
    GO
    /****** Object:  UserDefinedFunction [dbo].[GetCourseListByStudentID]  */  
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE FUNCTION [dbo].[GetCourseListByStudentID]
    (    
        -- Add the parameters for the function here
        @studentID int
    )
    RETURNS TABLE 
    AS
    RETURN 
    (
        -- Add the SELECT statement with parameter references here
        select c.courseid, c.coursename,c.Location, c.TeacherId
    from student s left outer join studentcourse sc on sc.studentid = s.studentid left outer join course c on c.courseid = sc.courseid
    where s.studentid = @studentID
    )

 

 

 

 

 

 

 

 

 

 

 

 

using (var ctx = new SchoolDBEntities())
        {
    //Execute TVF and filter result
            var courseList = ctx.GetCourseListByStudentID(1).Where(c => c.Location.SpatialEquals(DbGeography.FromText("POINT(-122.360 47.656)"))))
                            .ToList<GetCourseListByStudentID_Result>();

               
            foreach (GetCourseListByStudentID_Result cs in courseList)
                Console.WriteLine("Course Name: {0}, Course Location: {1}", 
                            cs.CourseName, cs.Location);
    }

 

posted @ 2017-03-26 15:02  蓝平凡  阅读(311)  评论(0编辑  收藏  举报