簡體   English   中英

使用Entity Framework在LINQ Web服務中選擇特定的列

[英]Select a specific column in LINQ web service with Entity Framework

我只想從包含列IDNameGenderMarkUp的簡單表中從實體框架僅返回一個稱為Name的列。

當我運行以下代碼時,出現以下錯誤:

無法將類型'System.Collections.Generic.List隱式轉換為'System.Collection.Generic.List'

碼:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using SimpleWebApplication;
using System.Configuration;

namespace SimpleWebApplication
{
    /// <summary>
    /// Summary description for SimpleWebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class SimpleWebService : System.Web.Services.WebService
    {


        [WebMethod]
        public List<tblStudent> getAllLINQ()
        {
            List<string> studentList = new List<string>();
            try
            {
                using (TestingEntities database = new TestingEntities())
                {
                    var studentData = (from table in database.tblStudents select table).Select(u => new { u.Name });
                    studentList = studentData.ToList();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return studentList;
        }


    }
}

如果您只需要名稱,則僅選擇名稱。

var query = from table in database.tblStudents select Name;

並且您的列表應該是string列表,因為名稱是字符串。

List<string> studentList = new List<string>();

您可以像這樣將結果分配給列表:

studentList = query.ToList();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM