簡體   English   中英

無法創建類型為“匿名類型”的常量值。 在此上下文中僅支持原始類型或枚舉類型兩個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

我有一個follwoing控制器,已在其中構建了兩個Linq查詢,以合並為一個以填充下拉列表。 我這樣做是因為數據來自兩個不同的上下文。

方法是:

    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部分獲得以下錯誤

無法創建類型為“匿名類型”的常量值。 在此上下文中僅支持原始類型或枚舉類型。

所有者類:

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類別:

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

    public string PostName { get; set; }
}

問題在於以下代碼行:

Owners = owners.ToList();

在這里,您對具有兩個屬性的匿名類型的對象序列調用List

Owners的類型是IEnumerable<Owner> 話雖如此,如果您稍微重構一下linq查詢,就會得到想要的。

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

認為 Owner具有兩個屬性, PostIdPostName 它可以具有更多的屬性,但至少應具有這兩個屬性。

現在,您的查詢返回IEnumerable<Owner> ,這是預期的類型。

您不能擁有具有匿名類型屬性的強類型。 更改owners查詢,以返回Owner對象的集合或創建僅包含所需屬性的單獨的強類型。

我收到一個異常“無法創建類型為'Anonymous type'的常量值。在此上下文中僅支持原始類型或枚舉類型。”

原因是因為您試圖將內存查詢( Posts )注入到EF查詢中。 由於您不使用Posts您可以合並查詢:

    var owners = from c in db.Owners
                 join a in Peopledb.Post
                     on c.PostId equals a.PostId
                 where a.Dormant == false
                 select new OwnerPost { OwnerId = c.OwnerId, PostName = a.PostName };

我最終通過將代碼調整為以下內容解決了這個問題:

視圖模型:

    [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);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM