-1

I can take value of this query in a variable but when I am trying to take value of this query in the "artistList " it shows error :Error Message

This is the model :

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

In Controller page I take a list like :

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

This is the query :

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();
  • You want to return list of tblArtist but you create an anonymous type in select clause. Just change it into select new tblArtist { ArtistId = ta.ArtistId, ... }. – Bartek Falkowski Jun 17 '16 at 06:52

2 Answers2

0

You select an anonymous type with this:

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

Linq cannot convert that anonymous type implicitly to tblArtist which it expects in the list of List<tblArtist>.

Instead of creating a new anonymous type, just create the tblArtist:

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

EDIT

Or even better, I suspect that your context.tblArtists already is a collection of the type tblArtist, so you can just change the select new to select ta , then the result already is of the type you need.

devqon
  • 13,818
  • 2
  • 30
  • 45
  • Thanx for your reply @devqon. But I need to take value in this list "artistList " because In .cshtml page it is used. Can you tell me any other way or any other type of query? Thank you – Sourav Kuila Jun 17 '16 at 06:59
  • With my answer, you can just leave the rest as is. Just do the `.ToList()` and it works – devqon Jun 17 '16 at 07:20
0

If you want records of only "tblArtists" table, then you can write like this;

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(); 
Kinjal Gohil
  • 958
  • 9
  • 15