nayan chowdhury
nayan chowdhury

Reputation: 283

Selecting some columns all data from a table using LINQ/lambda .net MVC?

I just want to select some specific columns from a table using LINQ or lambda expressions and then send the table to the client using Tempdata. But it's not working for me.

my controller =>(using LINQ)

TempData["Courses"] = (from a in db.Courses select new { a.name, a.vendor_heading }).ToList();

and using lambda expression=>

TempData["Courses"] = db.Courses.Select(x => new { x.name, x.vendor_heading }).ToList();

It seems perfect for me but I don't know why it's not working. but if I use =>

TempData["Courses"] = db.Courses.ToList();

It's absolutely working for me. (above I try to select * from Table) when I try to send the full table, it's not having any issue. But if I want to send a specific column, I have the issue. I need to send specific column => because I need only those columns.

In my view side=>

<div>
     @Html.LabelFor(model => model.Course.vendor_heading, "Vendor Name", new { @class = "control-label" })
     @{
       var VendorName = CoursesTable.Where(x => x.name == Model.name).Select(x => x.vendor_heading).First();
      }
     @Html.TextBox("vendor_heading", VendorName, new { @style = "border-radius:3px;", @class = "form-control", @placeholder = Html.DisplayNameFor(m => m.Course.vendor_heading), @autocomplete = "off", @readonly = "readonly" })
</div>

@{
IEnumerable<Course> CoursesTable = TempData["Courses"] as   IEnumerable<Course>;
 }

I mentioned above that if I send specific column it gives me error like=> enter image description here my model=>

public class Course
{
    [MaxLength(700, ErrorMessage = "The Max Length For Heading Is 700 Character!")]
    [Required(ErrorMessage="Vendor Name Is Required!")]
    [Display(Name = "Vendor Heading")]
    public string vendor_heading { get; set; }
    /// <summary>
    /// //////////
    /// </summary>
    [Key]
    [Required(AllowEmptyStrings = false, ErrorMessage = "Course Name Is Required!")]
    [MaxLength(700, ErrorMessage = "The Max Length For Course Name Is 700 Character!")]
    [Display(Name = "Course Name")]
    public string name { get; set; }
    /// <summary>
    /// //////////
    /// </summary>
    [Required(AllowEmptyStrings = false, ErrorMessage = "Course Code Is Required!")]
    [MaxLength(200, ErrorMessage = "The Max Length For Course Code Is 200 Character!")]
    [Display(Name = "Course Code")]
    public string code { get; set; }
    /// <summary>
    /// //////////
    /// </summary>
    [Required(AllowEmptyStrings = false, ErrorMessage = "Picture Is Required!")]
    [MaxLength(1000, ErrorMessage = "The Max Length For Pic Path Is 1000 Character!")]
    [Display(Name = "Pic")]
    public string pic_path { get; set; }
    /// <summary>
    /// /////////
    /// </summary>
    [Required(ErrorMessage = "Adding date Is Required!")]
    [DataType(DataType.Date, ErrorMessage = "Invalid Date!")]
    [Display(Name = "Adding Date")]
    public DateTime adding_date { get; set; }
    /// <summary>
    /// /////////
    /// </summary>
    [MaxLength(5000, ErrorMessage = "The Max Length For Course Details Is 5000 Character!")]
    [Display(Name = "Course Details")]
    public string details { get; set; }

    //relationship With  other tables-------
    [ForeignKey("vendor_heading")]
    public Vendor Vendor { get; set; }
    public List<Batche> Batches { get; set; }
}

I don't know if I could explain my problems to you guys properly. If not, let me know and if did, please help me....

Upvotes: 0

Views: 665

Answers (2)

Karthik Elumalai
Karthik Elumalai

Reputation: 1612

As you said , it should work with tempdata also.

Try with the following change:

Thats, in your code itself put the casting code ,before the line, where the error occurs, because this is the order in which the code will be executed.

 @{
      if(TempData["Courses"] != null)
{
    IEnumerable<Course> CoursesTable = TempData["Courses"] as   IEnumerable<Course>;
}
     }




    <div>
         @Html.LabelFor(model => model.Course.vendor_heading, "Vendor Name", new { @class = "control-label" })
         @{
           var VendorName = CoursesTable.Where(x => x.name == Model.name).Select(x => x.vendor_heading).First();
          }
         @Html.TextBox("vendor_heading", VendorName, new { @style = "border-radius:3px;", @class = "form-control", @placeholder = Html.DisplayNameFor(m => m.Course.vendor_heading), @autocomplete = "off", @readonly = "readonly" })
    </div>

Hope this also will work for you as you thought,

kindly let me know your feedbacks

Thanks karthik

Upvotes: 0

Simon
Simon

Reputation: 784

In your controller get the vendor name you want to display:

ViewBag["VendorName"] = db.Courses.Where(x => x.name == Model.name).Select(x => x.vendor_heading).First();

Then in your View use

@Html.TextBox("vendor_heading", ViewBag["VendorName"], new { @style = "border-radius:3px;", @class = "form-control", @placeholder = Html.DisplayNameFor(m => m.Course.vendor_heading), @autocomplete = "off", @readonly = "readonly" })

Upvotes: 1

Related Questions