如何在模型List中获取LINQ查询的值

时间:2016-06-17 06:50:20

标签: asp.net-mvc linq asp.net-mvc-4

我可以在变量中获取此查询的值,但是当我尝试在" artistList"中获取此查询的值时它显示错误:Error Message

这是模型:

public partial class tblArtist
    {
        public tblArtist()
        {
            this.tblArtistCategoryMappings = new HashSet<tblArtistCategoryMapping>();
            this.tblArtistGroupMembers = new HashSet<tblArtistGroupMember>();
            this.tblArtistPortfolios = new HashSet<tblArtistPortfolio>();
            this.tblArtistRatings = new HashSet<tblArtistRating>();
            this.tblArtistOverviews = new HashSet<tblArtistOverview>();
            this.tblArtistSchedules = new HashSet<tblArtistSchedule>();
        }

        public int ArtistId { get; set; }
        public string ArtistName { get; set; }
        public Nullable<System.DateTime> DateOfBirth { get; set; }
        public string Sex { get; set; }
        public string HouseNumber { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Country { get; set; }
        public string Pin { get; set; }
        public string Email { get; set; }
        public string Website { get; set; }
        public string Phone { get; set; }
        public string Mobile { get; set; }
        public Nullable<bool> IsActive { get; set; }
        public Nullable<bool> IsGroup { get; set; }
        public Nullable<bool> IsFeatured { get; set; }
        public Nullable<System.DateTime> AddedOn { get; set; }
        public string AboutMe { get; set; }
        public Nullable<decimal> MinmumRates { get; set; }
        public string FB_FanLink { get; set; }
        public Nullable<int> FK_UserId { get; set; }
        public Nullable<bool> IsApproved { get; set; }

        public virtual ICollection<tblArtistCategoryMapping> tblArtistCategoryMappings { get; set; }
        public virtual ICollection<tblArtistGroupMember> tblArtistGroupMembers { get; set; }
        public virtual ICollection<tblArtistPortfolio> tblArtistPortfolios { get; set; }
        public virtual ICollection<tblArtistRating> tblArtistRatings { get; set; }
        public virtual ICollection<tblArtistOverview> tblArtistOverviews { get; set; }
        public virtual tblUser tblUser { get; set; }
        public virtual ICollection<tblArtistSchedule> tblArtistSchedules { get; set; }
    }

在控制器页面中,我选择了一个列表:

List<tblArtist> artistList = new List<tblArtist>();

这是查询:

artistList = (from ta in context.tblArtists
                                    join tm in context.tblArtistCategoryMappings on ta.ArtistId equals tm.FK_ArtistId
                                    join tc in context.tblArtistCategories on tm.FK_CategoryId equals tc.ArtistCategoryId
                                    join tct in context.tblArtistCategoryTypes on tc.FK_ArtistCategoryTypeId equals tct.ArtistCategoryTypeId
                                    where tct.ArtistCategoryTypeId == TypesId && ta.IsFeatured == true
                                    select new
                                    {
                                        ta.AboutMe,
                                        ta.AddedOn,
                                        ta.Address,
                                        ta.ArtistId,
                                        ta.ArtistName,
                                        ta.City,
                                        ta.Country,
                                        ta.DateOfBirth,
                                        ta.Email,
                                        ta.FB_FanLink,
                                        ta.FK_UserId,
                                        ta.HouseNumber,
                                        ta.IsActive,
                                        ta.IsApproved,
                                        ta.IsFeatured,
                                        ta.IsGroup,
                                        ta.MinmumRates,
                                        ta.Mobile,
                                        ta.Phone,
                                        ta.Pin,
                                        ta.Sex,
                                        ta.State,
                                        ta.Website
                                    }).ToList();

2 个答案:

答案 0 :(得分:0)

您可以选择匿名类型:

select new
    {
        ta.AboutMe,
        ta.AddedOn,
        // ..
    }

Linq无法将该匿名类型隐式转换为tblArtist列表中的List<tblArtist>

不要创建新的匿名类型,只需创建tblArtist

select new tblArtist
    {
        AboutMe = ta.AboutMe,
        AddedOn = ta.AddedOn,
        // ..
    }

修改

或者甚至更好,我怀疑您的context.tblArtists已经是tblArtist类型的集合,所以您只需将select new更改为select ta,然后结果已经是您需要的类型。

答案 1 :(得分:0)

如果您只想录制&#34; tblArtists&#34;表,然后你可以这样写;

artistList = (from ta in context.tblArtists
                                    join tm in context.tblArtistCategoryMappings on ta.ArtistId equals tm.FK_ArtistId
                                    join tc in context.tblArtistCategories on tm.FK_CategoryId equals tc.ArtistCategoryId
                                    join tct in context.tblArtistCategoryTypes on tc.FK_ArtistCategoryTypeId equals tct.ArtistCategoryTypeId
                                    where tct.ArtistCategoryTypeId == TypesId && ta.IsFeatured == true
                                    select ta).ToList();