无法创建类型为“匿名类型"的常量值.在此上下文中仅支持原始类型或枚举类型两个db Linq查询 [英] Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context two db Linq query

查看:155
本文介绍了无法创建类型为“匿名类型"的常量值.在此上下文中仅支持原始类型或枚举类型两个db Linq查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下控制器,我在其中构建了两个Linq查询,这些查询合并为一个以填充下拉列表.我这样做是因为数据来自两个不同的上下文.

I have the follwoing controller which I have built two Linq queries in to merge into one to populate a drop down list. I have done this because data is coming from two different contexts.

方法是:

    private PeopleContext Peopledb = new PeopleContext();
    private IARContext db = new IARContext();        

    public ViewResult CreateAsset()
        {
        var Posts = (from a in Peopledb.Posts
                     orderby a.PostName
                     where a.Dormant == false
                     select new { a.PostId, a.PostName }).ToArray();

        var owners = from c in db.Owners
                     join a in Posts on c.PostId equals a.PostId
                     select new OwnerPost { OwnerId = c.OwnerId, PostName = a.PostName };

        CreateAssetViewModel viewModel = new CreateAssetViewModel
        {
            Asset = new Asset(),               
            Owners = owners.ToList(),
        };

        return View(viewModel);
    }

视图模型为:

public class CreateAssetViewModel
    {
    public int AssetId { get; set; }

    public Asset Asset { get; set; }

    [Required]
    [Display(Name = "Owner")]
    public int OwnerId { get; set; }
    public IEnumerable<OwnerPost> Owners { get; set; }

    }

我在CreateAssetViewModel viewModel = new CreateAssetViewmodel部分获得以下错误

I get the following error on the CreateAssetViewModel viewModel = new CreateAssetViewmodel section

无法创建类型为匿名类型"的常量值.在这种情况下,仅支持基本类型或枚举类型.

Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context.

所有者类:

public class Owner
{
    public int OwnerId { get; set; }

    [Column(TypeName = "int")]
    public int PostId { get; set; }

    [Column(TypeName = "bit")]
    public bool Dormant { get; set; }

    [Column(TypeName = "datetime2")]
    public DateTime Created { get; set; }

    public virtual ICollection<Asset> Assets { get; set; }

    public People.Models.Post Post { get; set; }
}

邮递班:

public class Post
{
    public int PostId { get; set; }

    [StringLength(50)]
    [Column(TypeName = "nvarchar")]
    public string PostName { get; set; }

    [Column(TypeName = "bit")]
    public bool Dormant { get; set; }

    [StringLength(350)]
    [Column(TypeName = "nvarchar")]
    public string Description { get; set; }

    public virtual ICollection<Contract> Contracts { get; set; }

    public virtual ICollection<Owner> Owners { get; set; }
}

OwnerPost类:

OwnerPost Class:

public class OwnerPost
{
    public int OwnerId { get; set; }

    public string PostName { get; set; }
}

推荐答案

我最终通过将代码调整为以下内容来解决了这个问题:

I solved this in the end by adjusting the code to the following:

Viewmodel:

Viewmodel:

    [Required]
    [Display(Name = "Owner")]
    public int OwnerId { get; set; }
    public List<OwnerPost> Owners { get; set; }

控制器:

    [HttpGet]
    public ViewResult CreateAsset()
    {
        var posts = new List<People.Models.Post>(Peopledb.Posts);

        var owners = new List<Owner>(db.Owners);

        var ownerposts = from c in posts
                         join d in owners on c.PostId equals d.PostId
                         select new OwnerPost { OwnerId = d.OwnerId, PostName = c.PostName };

        CreateAssetViewModel viewModel = new CreateAssetViewModel
        {
            Asset = new Asset(),
            Owners = ownerposts.ToList(),
        };

        return View(viewModel);
    }

这篇关于无法创建类型为“匿名类型"的常量值.在此上下文中仅支持原始类型或枚举类型两个db Linq查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆