با سلام
چگونه میتوانم یک کویری که حاصل از دیتایس است را در لیست قرار دهم
کدهای من به شکل زیر است
List<Tbl_Products> list=new List<Tbl_Products>(); foreach (var item in q0) { var q = (from a in DB.Tbl_Products where a.CatID == item.ID select a).OrderByDescending(o => o.ID).ToList(); var query = (from s in q let e = i++ where i >= fro && i <= to select new { Row_Number = i, s.ID, s.ProductName, s.Description, s.Image, s.picViewPost, s.VisitCount, s.Autcher, s.KeyWordSeo, s.Company, s.CountProduct, s.OrderCount, s.Rice, s.DescriptionSEO, s.Takhfif, s.RiceStroke }).OrderByDescending(o => o.ID).ToList();
با توجه به کد بالا میخواهم مقدار query را در list قرار دهم
البته ناگفته نماند که من کد زیر را امتحان کردم ولی خطا داد
list.AddRange(query);
و خطا :
cannot convert from 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable<car.Tbl_Products
و خطای
The best overloaded method match for 'System.Collections.Generic.List<car.Tbl_Products>.AddRange(System.Collections.Generic.IEnumerable<car.Tbl_Products>)' has some invalid arguments
شما در کوئری خود در حال باز گردانی یک anonymous type هستید و نمیتوان آن را به لیستی از نوع `Tbl_Products` اضافه کنید. برای این که بتوانید آن ها را به لیست مورد نظر اضافه کنید نوع بازگردانی شده باید از جنس `Tbl_Products` باشد.
هنگام انجام select باید `tbl_Product` را بازگردانی کنید:
var query = (from s in q let e = i++ where i >= fro && i <= to select new Tbl_Products { ID = s.ID, ProductName = s.ProductName, // more code }).OrderByDescending(o => o.ID).ToList();
متشکرم
ولی من که میخوام کد Row_Number = i, در کویری باشد چکار کنم؟
چون این خط کد برای پیج بندی است
منتظر پاسخ شما هستم
با تشکر
یک view model بسازید و لیست را از نوع View model قرار دهید
اینگونه میتوانید هر آیتمی بخواهید قرار دهید
ببخشید منظور شما رو از View model متوجه نشدم
بنده asp WebForm کار میکنم نه MVC
خیلی کارم گیره باتشکر از همکاریتون
دوست من ViewModdel برای MVC نیست
یک کلاس برای دریافت نتیجه کوئری میسازید و از اون استفاده میکنید
بعد از Select New نام کلاس جدید را بزارید و مقادیر را از کوئری پر کنید
ممنون از لطف بی کران شما
میشه کد مربوطه رو قرار دهید
متشکرم
این کلاس رو ساختم
public class roww { public int Row_Number; public int ID { get; set; } public string ProductName { get; set; } public string RiceStroke { get; set; } public string Rice { get; set; } public string Takhfif { get; set; } public string Description { get; set; } public string Image { get; set; } public string picViewPost { get; set; } public string DatePublish { get; set; } public Nullable<int> OrderCount { get; set; } public Nullable<int> VisitCount { get; set; } public string KeyWordSeo { get; set; } public string DescriptionSEO { get; set; } public string Autcher { get; set; } public Nullable<bool> Selected { get; set; } public Nullable<int> CatID { get; set; } public string Info { get; set; } public Nullable<int> Sort { get; set; } public string Company { get; set; } public Nullable<int> CountProduct { get; set; } public Nullable<int> AnbarDari { get; set; } }
حال وقتی استفاده میکنم
خطا میدهد
List<Tbl_Products> list=new List<Tbl_Products>(); foreach (var item in q0) { var q = (from a in DB.Tbl_Products where a.CatID == item.ID select a).OrderByDescending(o => o.ID).ToList(); var query = (from s in q let e = i++ where i >= fro && i <= to select new roww { Row_Number = i, ID = s.ID, ProductName= s.ProductName, Description=s.Description, Image=s.Image, picViewPost=s.picViewPost, VisitCount=s.VisitCount, Autcher=s.Autcher, KeyWordSeo=s.KeyWordSeo, Company=s.Company, CountProduct=s.CountProduct, OrderCount=s.OrderCount, Rice=s.Rice, DescriptionSEO=s.DescriptionSEO, Takhfif=s.Takhfif, RiceStroke=s.RiceStroke }).OrderByDescending(o => o.ID).ToList(); list.AddRange(query);//************************************************************** } MyRepeater.DataSource = list; MyRepeater.DataBind();
خطای من در این خط است
list.AddRange(query);
و خطا این است
Error 1 The best overloaded method match for 'System.Collections.Generic.List<car.Tbl_Products>.AddRange(System.Collections.Generic.IEnumerable<car.Tbl_Products>)' has some invalid arguments
و
Error 2 Argument 1: cannot convert from 'System.Collections.Generic.List<car.ClassDB.roww>' to 'System.Collections.Generic.IEnumerable<car.Tbl_Products>'
List<roww> list=new List<roww>(); foreach (var item in q0) { var q = (from a in DB.Tbl_Products where a.CatID == item.ID select a).OrderByDescending(o => o.ID).ToList(); var query = (from s in q let e = i++ where i >= fro && i <= to select new roww() { Row_Number = i, ID = s.ID, ProductName= s.ProductName, Description=s.Description, Image=s.Image, picViewPost=s.picViewPost, VisitCount=s.VisitCount, Autcher=s.Autcher, KeyWordSeo=s.KeyWordSeo, Company=s.Company, CountProduct=s.CountProduct, OrderCount=s.OrderCount, Rice=s.Rice, DescriptionSEO=s.DescriptionSEO, Takhfif=s.Takhfif, RiceStroke=s.RiceStroke }).OrderByDescending(o => o.ID).ToList(); list.AddRange(query);//************************************************************** } MyRepeater.DataSource = list; MyRepeater.DataBind();
متشکرم
بلحق و النصاف شما stackoverflow.com ایران هستین
یک دنیا تشکر
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)