我非常新于Linq和Entity Framework,我的下面的代码有问题。我得到的错误是:
无法将类型为“<>f__AnonymousType1`2[System.Int64,System.String]”的对象强制转换为类型“ConsoleApplication1.Profile”。
我的代码如下:
static void Main(string[] args)
{
ProfileEntitiesContext obj = new ProfileEntitiesContext();
List<Profile> list = new List<Profile>();
var test = (from c in obj.Customer
join a in obj.Address
on c.ProfileId
equals a.Customer.ProfileId
select new
{
CustomerProfileId = c.CustomerProfileId,
FirstName = c.FirstName
AddressLine1 = a.Line1
}).ToList().Cast<CustomerConfidentialProfile>();
foreach(var cust in test) // Unable to cast object of type '<>f__AnonymousType1`2
//[System.Int64,System.String]' to type 'ConsoleApplication1.Profile'.
{
list.Add(cust);
}
}
public class Profile
{
int _CustomerProfileId;
string _FirstName;
string _AddressLine1;
public int CustomerProfileId
{
get { return _CustomerProfileId; }
set { _CustomerProfileId = value; }
}
public string FirstName
{
get { return _FirstName; }
set { _FirstName = value; }
}
public string AddressLine1
{
get { return _AddressLine1; }
set { _AddressLine1= value; }
}
}
任何帮助都将不胜感激。