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 entities.processlists
where recordset.ProcessName == processname
select recordset.ServerName, recordset.ProcessID, recordset.Username;
Obviously, this doesn't even compile. What is the correct syntax? I also tried method based, and even tough this syntax seems correct, when accessing it throws an 'Unable to cast the type 'Anonymous type' to type 'AIM.PInfo'. LINQ to Entities only supports casting EDM primitive or enumeration types.' exception.
Any ideas?
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 })
.Cast<PInfo>().ToList();
This question is related to
c#
.net
entity-framework
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();
You either want to select an anonymous type:
var dataset2 = from recordset
in entities.processlists
where recordset.ProcessName == processname
select new
{
recordset.ServerName,
recordset.ProcessID,
recordset.Username
};
But you cannot cast that to another type, so I guess you want something like this:
var dataset2 = from recordset
in entities.processlists
where recordset.ProcessName == processname
// Select new concrete type
select new PInfo
{
ServerName = recordset.ServerName,
ProcessID = recordset.ProcessID,
Username = recordset.Username
};
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();
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();
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
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
Source: Stackoverflow.com