S
S
sdfdgfhgfh2018-10-16 22:17:46
ASP.NET
sdfdgfhgfh, 2018-10-16 22:17:46

Can't implicitly convert type "string"?

There was a simple request to extract a list in descending order from the database with pagination and search on the page:

spoiler
public async Task<IActionResult> Online(string name, int page = 1)
        {
            int pageSize = 30;   // количество элементов на странице                            

            IQueryable<Chr> source = from c in db.Chr
                                           orderby c.RCount, c.Level
                                           select new Chr()
                                           {
                                               Name = c.Name,
                                           };

            if (!String.IsNullOrEmpty(name))
            {
                source = source.Where(p => p.Name.Contains(name));
            }

            var count = await source.CountAsync();
            var items = await source.Skip((page - 1) * pageSize).Take(pageSize).ToListAsync();

            // формируем модель представления
            IndexViewModel model = new IndexViewModel
            {
                PageViewModel = new PageViewModel(count, page, pageSize),
                FilterViewModel = new FilterViewModel(db.Chr.ToList(), name),
                Chr = items
            };

            return View(model);
        }


indexviewmodel:
spoiler
public class IndexViewModel
    {
        public IEnumerable<Chr> Chr { get; set; }           
        public PageViewModel PageViewModel { get; set; }
        public FilterViewModel FilterViewModel { get; set; }       
    }


Recently it was required to take one more table from base. What I did:
spoiler
public async Task<IActionResult> Online(string name, int page = 1)
        {
            int pageSize = 30;   // количество элементов на странице                            

            IQueryable<Chr> source = from c in db.Chr
                                           join g in db.GMember
                                           on c.Name equals g.Name into gg
                                           from ggg in gg.DefaultIfEmpty()

                                           orderby c.RCount, c.Level
                                           select new IndexViewModel()
                                       {
                                           Chr = c.Name,
                                           GMember = ggg.G_Name
                                       };


            if (!String.IsNullOrEmpty(name))
            {
                source = source.Where(p => p.Name.Contains(name));
            }

            var count = await source.CountAsync();
            var items = await source.Skip((page - 1) * pageSize).Take(pageSize).ToListAsync();

            // формируем модель представления
            IndexViewModel model = new IndexViewModel
            {
                PageViewModel = new PageViewModel(count, page, pageSize),
                FilterViewModel = new FilterViewModel(db.Chr.ToList(), name),
                Chr = items
            };

            return View(model);
        }


I end up with 2 errors:
Не удается неявно преобразовать тип "string" в "System.Collections.Generic.IEnumerable<Web.Models.DataBase.Chr>"

Не удается неявно преобразовать тип "string" в "System.Collections.Generic.IEnumerable<Web.Models.GMember>"

In lines
Chr = c.Name,
GMember = ggg.G_Name

Please tell me what I did wrong.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hutamari, 2018-10-17
@hutamari

First, in the last code example, it should be:
instead of Second, there is an assignment in IEnumerable to a variable with type string, instead:

select new SomeType()
                                       {
                                           Chr = new Chr{ Name = c.Name},
                                           GMember = new GMember { Name = ggg.G_Name}
                                       };

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question