I am developing an asp.net mvc application with NHibernate and I have a query where I would like to know how can I convert rows in columns?
I have a dynamic system with a model like this:
// It's a kind of metadata
public class Field
{
public virtual long Id { get; set; }
public virtual string Name { get; set; }
}
// the value is here with respective field
public class FieldValue
{
public virtual long Id { get; set; }
public virtual Field Field { get; set; }
public virtual string Value { get; set; }
}
I would like to know how can I get a result where columns are the Field objects and values are FieldValue objects, should I create a ViewModel? or a way to do it with asp.net mvc?
I can do a query to get a result like this:
But I would like to do a query like this (or a way to create a result on my View on the asp.net mvc):
Thanks
I think you are going to need to get fancy/familiar with GroupBy(). If you Group By the Field, you "group" all the values for a field then move to the next one. The pseudo-code would be something like the following
#model IEnumerable<FieldValue>
foreach(var fields in Model.GroupBy(x=>x.Field.Name){
<h2>fields.key</h2>
<ul>
foreach(var fieldValue in fields){
<li>#fieldValue.Value</li>
}
</ul>
}
This should render something like
Field 1
some value 1
some value 2
Field 2
some value 3
some value 4
etc.
By using this approach, you can control the formatting however you want to get the desired output.
Not clear with the question. if you want to transform rows into columns - you can use pivot. But i would suggest instead of operating on resultset to transpose, let it return the transposed result set. I meant handle the conversion of rows into columns in your query itself
Related
In the tutorials I've walked through around creating an API in C#, I've gone through creating an HTTP PUT command for updating records in a table contained in a database.
The examples I've seen, essentially, I create a DTO around the fields that can be updated in that table. For example, I have a class that looks like the following:
public class UpdateTablenameDTO
{
public int ID { get; set; }
public int IsActive { get; set; }
public int IsDeleted { get; set;}
...
I then built a controller and all of the fields in my UpdateTablenameDTO appear as elements expected when I do an update.
What I wanted to know is there a proper approach to not requiring all of the elements in the Update DTO when doing the Update call? When I send my payload to include only ID and IsActive, it complained that it needed the rest of my fields. When I think this through, there could be a situation that a user is sitting on a screen with an old state but with a specific update that they want to send through (i.e. make the record inactive).
I don't necessarily want to update all of the elements, really only the specific changes, which would be the only thing I would want to send, along with the ID for identification. I suppose the way I could do this is to check if the record has changed since the user last viewed it upon updating, but I wanted to make sure I wasn't missing something obvious for this kind of scenario.
You can use Nullable value types to indicate that a property is "optional". The deserializer on the webapi side will keep the property as null when no value is provided. You can define the receiving DTO as follow:
public class UpdateTablenameDTO
{
public int ID { get; set; } // still required
public int? IsActive { get; set; } // now it's optional
public int? IsDeleted { get; set;} // optional as well
}
When you provide the JSON body as {"ID":5, "IsActive": 20} then you get an UpdateTablenameDTO instance as follow:
new UpdateTablenameDTO {
ID = 5,
IsActive = 20,
// IsDeleted = null
}
When you receive such an object, you can simply do a check against null or check the HasValue property to see, if there was a value in the request.
if (data.IsActive.HasValue) {
// do something with "data.IsActive.Value"
}
I want to retrieve all tickets whose badge number is present in the list
i have a listA
public class Li
{
public string s { get; set; }
public string t { get; set; }
public string b { get; set; }
}
i have a method
i try this condition but it doesn't work,
doesn't recognize tostring for the first and doesn't recognize the list
Any idea how solve my problem?
This should work:
where listeLigneXls.Select(lgn => lgn.badge).Contains(chb.NUM_BADGE)
Need to change in this code block
where listeLigneXls.Select(lgn =>lgn.badge).Contains(chb.NUM_BADGE)
Contain is extension method of list of primitive data type
I change the method I get the values of the badge number and I would like to use it in my second query but I do not know how
I am trying to build a helper class for Datatables and need some advice.
I want to create a helper that I can pass in an object that represents the settings for a DataTables that is typed by a specific model. This object would contain a collection of expressions that represent the columns to be used by the helper. As part of the DataTables object there will be a property that holds the URL that servers up the data for the grid. This will effectively be the sAjaxSource parameter. As part of the URL call, a JSON package will be sent that will contains the information about the columns. This whole section, I understand how to build. Basically, I would build out the DataTables JavaScript code including column definitions and create a JSON object that represents the columns I want to pass in the URL.
The area I needs some advice on is building the data for the specific columns once hte call is made to the server / controller. So my controller may look like this:
public ActionResult GetUsersList(IEnumerable<DatatableColumnJson> columns)
{
var users = _someUserRepository.GetAll();
foreach (var user in users)
{
//Here I would build the JSON that DataTables needs to render the grid.
//I would iterate through the users and pull out the data for the specific columns
}
return Json(someCompleteDataTablesObject, JsonRequestBehavior.AllowGet);
}
Supporting Classes:
public class DatatableColumnJson
{
public string Name { get; set; }
public string Visible { get; set; }
}
public class User
{
public string UserName { get; set; }
public string Email { get; set; }
}
So I first thought I could use reflection, but that seems to be a little intensive, maybe. Another thought was to transform the column names into expressions to possibly pull out the data. I want something efficient and that doesn't have a lot of overhead, but maybe I am asking too much.
I am using ASP.Net MVC 3 and I need to create a single drop down list which contains items that relate to multiple database tables.
Normally, if I need to do a drop down list for a single data type I can easily use the ID as the "value" for each drop down option and would do something like this:
#Html.DropDownListFor(x => x.SelectedID, Model.GetMyList())
But now I want to mix up multiple data types. So lets say for this example I want to create a single list to represent something like "Owner" and this can be either a "User" or a "Customer". In this example, both User and Customer are separate database tables and therefore the ID value alone is not enough to identify them correctly.
So what are the best ways to achieve such functionality?
Straight off the top of my head, my first thoughts are to create a "custom" value string which could then be parsed server side to work out the ID and data type, something like...
"USER|1"
"CUSTOMER|1"
I know I can make this work, but am I making this more complicated than it needs to be? Is there a built-in or advised way of doing this?
In your Model can you not do something like this:-
public class Model
{
public string Owner { get; set; }
public List<MyList> ListCollection { get; set; }
public class MyList
{
public int Id { get; set; }
public string Value { get; set; }
}
}
So then when you are checking which list item is selected you also have access to the "Owner" field which will tell you what table it belongs to ?
As nobody has come up with anything better, I can confirm that my original idea (as unwanted as it was) did the job.
When setting the value of the select options, a custom string should be created that can easily be parsed server side, this was achieved using a pipe separating the TYPE of entity, and the ID, for example:
"USER|1"
"USER|2"
"CUSTOMER|1"
"CUSTOMER|2"
Once the selected value is passed to the server, it can then be parsed something like the following:
string option = "USER|1";
string[] values = option.Split('|');
string entityType = values[0];
int entityId = Int.Parse(values[1]);
which can then be used something like this:
if(entityType == "USER")
UpdateUser(entityId);
else//CUSTOMER
UpdateCustomer(entityId);
I'm not sure where to look for this one... I've got a viewmodel that has an underlying DataRow providing part of the model. I want to display this information as a single record, in a vertical layout. I planned to use the DataGrid because I want the user to be able to add/delete/rename rows right across the DataTable despite only looking at one record. I'm not quite sure how to achieve this though. Example of what I'm expecting is below:
Source Data Table
ID, Name, Value
1, One, 1
2, Two, 2
Expected in my UI would be a table looking like the following
ID | 1
Name | One
Value | 1
You can expose the DataRow as a list of fields :
public class DataRowField
{
public int Index { get; set; }
public string Name { get; set; }
public object Value { get; set; }
}
public IEnumerable<DataRowField> Fields
{
get
{
return _dataRow.Table.Columns.Cast<DataColumn>()
.Select((column, i) => new DataRowField
{
Index = i,
Name = column.ColumnName,
Value = _dataRow[column]
});
}
}
Then you just need to bind your DataGrid to the Fields property