小编use*_*099的帖子

无法转换类型的对象<> f__AnonymousType1`2 [System.Int64,System.String]'为类型 'ConsoleApplication1.Profile'.

我对Linq和实体框架工作很新,我的下面的代码有问题.我收到了错误

无法转换类型的对象<> 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; …
Run Code Online (Sandbox Code Playgroud)

c# linq-to-entities

3
推荐指数
1
解决办法
2万
查看次数

标签 统计

c# ×1

linq-to-entities ×1