Select multiple columns using Entity Framework

前端 未结 7 1360
逝去的感伤
逝去的感伤 2021-01-31 07:10

Maybe an easy question, but can\'t find it easily so forgive me =) I try to select multiple columns. The statement I use is:

var dataset2 = from recordset in ent         


        
7条回答
  •  温柔的废话
    2021-01-31 08:11

    You can select to an anonymous type, for example

    var dataset2 = 
        (from recordset in entities.processlists 
        where recordset.ProcessName == processname 
        select new
        {
            serverName = recordset.ServerName,
            processId = recordset.ProcessID, 
            username = recordset.Username
        }).ToList();
    

    Or you can create a new class that will represent your selection, for example

    public class MyDataSet
    {
        public string ServerName { get; set; }
        public string ProcessId { get; set; }
        public string Username { get; set; }
    }
    

    then you can for example do the following

     var dataset2 = 
        (from recordset in entities.processlists 
        where recordset.ProcessName == processname 
        select new MyDataSet
        {
            ServerName = recordset.ServerName,
            ProcessId = recordset.ProcessID, 
            Username = recordset.Username
        }).ToList();
    

提交回复
热议问题