This is a migrated thread and some comments may be shown as answers.

Using Linq in column bound

9 Answers 329 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michel
Top achievements
Rank 1
Michel asked on 16 Jan 2017, 12:23 PM
Hi,

I don't know how I can use linq query in bounding a grid column to display a value.

If I use this code

    @(Html.Kendo().Grid<MYMODEL>(Model)
    .Name("myObject")
    .Columns(columns =>
    {
        columns.Bound(ViewBag["myList"].Where(p => p.Value == "2").Text);

I have an error : "cannot  use lamba expression without casting before.

Any help will be very much appreciated

9 Answers, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 16 Jan 2017, 03:02 PM
Hello Michel,

As the error states, you need to cast the object from the ViewData to a type that supports lambda expressions (for example, if it is a List: (ViewBag["myList"] as List<YourClass>).Where(p => p.Value == "2"))

Hope this helps.


Regards,
Konstantin Dikov
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Michel
Top achievements
Rank 1
answered on 16 Jan 2017, 03:44 PM

Thanks for this Konstantin. I need just to figure out the meaning of the message. Sorry.

But the thing that I want to achieve is a bit more complicated.

I have a list in my controller coming from a table in a database and I want to use the ViewBag for selecting the value in a list in editor popup (that works well) but want to display the LABEL and not CODE in the grid (even is CODE is stored in the database table).

Controller.cs

public async Task<IActionResult> Index()

      ViewData["myList"] = new SelectList(_context.MYTABLE.Where(m =>                                                                                (m.FIELDNAME == "MYTYPE")),"CODE", "LABEL");

Razor View

       ...

columns.Bound(model => model.myCode); // For testing purpose

        columns.Bound((((ViewBag["arType"] as List<SelectList>).Where(p => p.DataValueField == "LOC")) as SelectListItem).Text);
// Error when executing

Any suggestion ?

...

0
Michel
Top achievements
Rank 1
answered on 16 Jan 2017, 03:47 PM

 

Sorry some mistake were in the previous post.

Thanks for this Konstantin. I need just to figure out the meaning of the message. Sorry.
But the thing that I want to achieve is a bit more complicated.
I have a list in my controller coming from a table in a database and I want to use the ViewBag for selecting the value in a list in editor popup (that works well) but want to display the LABEL and not CODE in the grid (even if CODE is stored in the database table).
Controller.cs
public async Task<IActionResult> Index()
      ViewData["myList"] = new SelectList(_context.MYTABLE.Where(m =>                                                                                (m.FIELDNAME == "MYTYPE")),"CODE", "LABEL");
Razor View
       ...
columns.Bound(model => model.myCode); // For testing purpose
        columns.Bound((((ViewBag["myList"] as List<SelectList>).Where(p => p.DataValueField == "LOC")) as SelectListItem).Text);
// Error when executing
Any suggestion ?

0
Konstantin Dikov
Telerik team
answered on 17 Jan 2017, 05:18 AM
Hello Michel,

I am not sure that I understand what is the result that you want to achieve. Within the bound column you could pass a string value that will match the name of the field that will be bound to the column.

If you want the column to be bound to one field, but to display the value for another field, the records in the grid should contain both values. The first field will be bound to the column and the second field can be displayed through a ClientTemplate (for example: ClientTemplate("#=SecondFieldName#")).

Hope this helps.


Regards,
Konstantin Dikov
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Antony
Top achievements
Rank 1
answered on 15 Nov 2017, 11:02 AM

Hi Michel,

Were you able to find a workaround this situation? I've come across the same problem. 

0
Michel
Top achievements
Rank 1
answered on 15 Nov 2017, 12:47 PM

It is quite long time but in looking at my code here is the view (forgot the FooterTemplate if you don't need it):

columns.ForeignKey(model => model.CATEGORY_MEM, (System.Collections.IEnumerable)ViewBag.arCategory,"CODE_LFB", "LABEL_LFB") .ClientFooterTemplate("Total Branche : #=count#");

 

 

And in the Controller :

var category = _context.VG_LSTFEEDBACK_LFB.Where(f => f.NUMBER_LFB == "CAT").
  Select(f => new
  {
    CODE_LFB = f.CODE_LFB,
    LABEL_LFB = f.LABEL_LFB
  });
ViewData["arCategory"] = category.ToList();

 

The VG_LSTFEEDBACK_LFB is a SQL Table with different List value, it is why a use a where clause.

Hope it help's.

 

 

 

0
Michel
Top achievements
Rank 1
answered on 15 Nov 2017, 12:52 PM

SQL Server Database MPD

CREATE TABLE [dbo].[VG_LSTFEEDBACK_LFB](
 [GUID_LFB] [uniqueidentifier] NOT NULL,
 [NAME_LFB] [varchar](100) NULL,
 [NUMBER_LFB] [varchar](25) NULL,
 [CODE_LFB] [int] NOT NULL,
 [LABEL_LFB] [varchar](25) NULL,
 CONSTRAINT [PK_VG_LSTFEEDBACK_LFB] PRIMARY KEY CLUSTERED

0
Antony
Top achievements
Rank 1
answered on 15 Nov 2017, 12:52 PM
Thanks, using it as a foreign key worked. 
0
Stefan
Telerik team
answered on 17 Nov 2017, 07:37 AM
Hello, Michel, Antony,

Michel, thank you for sharing the code and helping the community it is highly appreciated.

Antony, I`m glad to hear that the foreign key was helpful.

I can also suggest checking our foreign key demo and resources as they may prove helpful: 

http://demos.telerik.com/aspnet-mvc/grid/foreignkeycolumn

Kendo.Mvc.UI.Fluent/GridColumnFactory

https://docs.telerik.com/aspnet-mvc/helpers/grid/how-to/editing/configure-foreignkey-columns-combobox-edit-template


Regards,
Stefan
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
Michel
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Michel
Top achievements
Rank 1
Antony
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or