Select multiple columns using Entity Framework

前端 未结 7 1358
逝去的感伤
逝去的感伤 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 07:53

    Why don't you create a new object right in the .Select:

    .Select(x => new PInfo{ 
        ServerName = x.ServerName, 
        ProcessID = x.ProcessID, 
        UserName = x.Username }).ToList();
    
    0 讨论(0)
  • 2021-01-31 07:59

    Here is a code sample:

    var dataset = entities.processlists
        .Where(x => x.environmentID == environmentid && x.ProcessName == processname && x.RemoteIP == remoteip && x.CommandLine == commandlinepart)
        .Select(x => new PInfo 
                     { 
                          ServerName = x.ServerName, 
                          ProcessID = x.ProcessID, 
                          UserName = x.Username 
                     }) AsEnumerable().
                   Select(y => new PInfo
                   {
                       ServerName = y.ServerName,
                       ProcessID = y.ProcessID,
                       UserName = y.UserName 
                   }).ToList();
    
    0 讨论(0)
  • 2021-01-31 08:05

    Indeed, the compiler doesn't know how to convert this anonymous type (the new { x.ServerName, x.ProcessID, x.Username } part) to a PInfo object.

    var dataset = entities.processlists
        .Where(x => x.environmentID == environmentid && x.ProcessName == processname && x.RemoteIP == remoteip && x.CommandLine == commandlinepart)
        .Select(x => new { x.ServerName, x.ProcessID, x.Username }).ToList();
    

    This gives you a list of objects (of anonymous type) you can use afterwards, but you can't return that or pass that to another method.

    If your PInfo object has the right properties, it can be like this :

    var dataset = entities.processlists
        .Where(x => x.environmentID == environmentid && x.ProcessName == processname && x.RemoteIP == remoteip && x.CommandLine == commandlinepart)
        .Select(x => new PInfo 
                     { 
                          ServerName = x.ServerName, 
                          ProcessID = x.ProcessID, 
                          UserName = x.Username 
                     }).ToList();
    

    Assuming that PInfo has at least those three properties.

    Both query allow you to fetch only the wanted columns, but using an existing type (like in the second query) allows you to send this data to other parts of your app.

    0 讨论(0)
  • 2021-01-31 08:10

    It's correct way to get data in specified type:

    var dataset = entities.processlists
             .Where(x => x.environmentID == environmentid && x.ProcessName == processname && x.RemoteIP == remoteip && x.CommandLine == commandlinepart)
             .Select(x => new { x.ServerName, x.ProcessID, x.Username })
             .ToList() /// To get data from database
             .Select(x => new PInfo()
                  { 
                       ServerName = x.ServerName, 
                       ProcessID = x.ProcessID, 
                       Username = x.Username 
                  });
    

    For more information see: The entity cannot be constructed in a LINQ to Entities query

    0 讨论(0)
  • 2021-01-31 08:10
    var test_obj = from d in repository.DbPricing
    join d1 in repository.DbOfficeProducts on d.OfficeProductId equals d1.Id
    join d2 in repository.DbOfficeProductDetails on d1.ProductDetailsId equals d2.Id
        select new
        {
        PricingId = d.Id,
        LetterColor = d2.LetterColor,
        LetterPaperWeight = d2.LetterPaperWeight
        };
    
    
    http://www.cybertechquestions.com/select-across-multiple-tables-in-entity-framework-resulting-in-a-generic-iqueryable_222801.html
    
    0 讨论(0)
  • 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();
    
    0 讨论(0)
提交回复
热议问题