Creating custom linq column names in list - c#

We are returning a generic List to a GridView, which then auto generates columns to show a report:
//Generate List
List<Stock> allStock = blStock_Collection.getAll();
//export custom view for report datagrid
return (from myStock in allStock
select new
{
myStock.Category,
myStock.Description,
myLowStock.UnitPrice,
myLowStock.CurrentQuantity
});
Our client has asked that we now provide multi-lingual support on our site (English & Polish), specifically the column headers on grids. We would therefore need to get rid of the auto generate option on all our data grids, and add in the translations manually for each column.
I was wondering is there a way to do this when generating the datasource, which would save us a hell of a lot of time, e.g. changing this line:
myStock.Category,
to something like:
languagePack.Category = myStock.Category,
This is of course throwing a 'Invalid anonymous type member declarator' error. Any advice?

Maybe I misread something, but if you just want to put your language into it (LanguagePack is just a class holding your values):
class LanguagePack
{
public int Category { get; set; }
public string Description { get; set; }
public decimal UnitPrice { get; set; }
public int CurrentQuantity { get; set; }
public int LanguageName { get; set; }
}
return (from myStock in allStock
select new LanguagePack
{
Category = myStock.Category,
Description = myStock.Description,
UnitPrice = myLowStock.UnitPrice,
CurrentQuantity = myLowStock.CurrentQuantity,
LanguageName = "Polish" // or english or whatever... maybe LanguageId or something corresponding to your model
});

Related

What's the correct way to reference tables using Code First with EF.Core for searching efficiently

Fairly new to EF.Core and I'm having some issues as my tables start getting more complex. Here's an example of what I have defined for my classes. Note ... there are many more columns and tables than what I have defined below. I've paired them down for brevity.
public class User
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool Active { get; set; }
}
Followed by
public class JournalEntry
{
public int Id { get; set; }
public int UserId { get; set; }
public string Details { get; set; }
public DateTime DateEntered { get; set; }
public virtual User User { get; set; }
}
I want to be able to issue the following query and INCLUDE the User Table so that I can then populate a ViewModel with columns from the User Table without having to do another lookup and also to sort the data while retrieving it:
public IQueryable<JournalEntry> GetByUser(int userId)
{
return _DbContext.JournalEntries.Where(j => j.UserId == userId)
.Include(u => u.User)
.OrderBy(u=> u.User.FirstName)
.ThenBy(j => j.DateEntered);
}
My controller would then have something similar to the following:
public IActionResult List(int userId)
{
var journalEntries = new _dbRepository.GetByUser(userId);
var myViewModel = new MyViewModel();
myViewModel.UserName = ($"{journalEntries.User.FirstName} {journalEntries.User.LastName}");
myViewModel.Entries = journalEntries;
etc ....
return View(myViewModel);
}
I'm loading the user's first and last name in the View Model and whatever other attributes from the various tables that are referenced. The problem that I'm having is that I'm getting errors on the Migration creation "Foreign key constraint may cause cycle or multiple cascade paths." And of course, if I remove the line reading public virtual User User { get; set; } from the JournalEntry class then the problem goes away (as one would expect).
I believe that the way I'm doing the models is incorrect. What would be the recommended way that I should code these models? I've heard of "lazy loading". Is that what I should be moving towards?
Thanks a bunch.
--- Val
Your query returns an IQueryable<JournalEntry> not a JournalEntry.
Change the code to get the user details from the first object:
var myViewModel.UserName = ($"{journalEntries.First().User.FirstName} {journalEntries.First().User.LastName}");
In the line above I'm calling First() on your journal entries collection and that would have a User. Then I can access FirstName and LastName.
Also, don't bother with LazyLoading since you are learning. It could cause select n+1 issues if used incorrectly

SQLite, Linq, Foreach: how to optimize performances?

I've developed an UWP app where I use a SQLite database to store datas that are synced.
Among these data, there a lot of tables that contain translated data. For example, for various cases, we have:
a "businness" table, which contains the id that is really used in the database
a "translation" table, which contains transaltion for the business table
The models of the "business" tables are defined like this:
public class Failure_Type : BasePoco
{
[PrimaryKey, NotNull]
public int failure_id { get; set; }
public int? function_type_id { get; set; }
public int? component_category_id { get; set; }
[MaxLength(200), NotNull]
public string description { get; set; }
public DateTime? disable_date { get; set; }
[Ignore]
public string _descriptionTr { get; set; }
}
The field "description" stores the english/default description, and the "_descriptionTr" field will store the translated description.
The models of the "translation" tables are defined like this:
public class Failure_Type_Translation : BasePoco
{
[PrimaryKey, NotNull]
public int failure_type_translation_id { get; set; }
public int? failure_type_id { get; set; }
[MaxLength(2)]
public string language { get; set; }
[MaxLength(200), NotNull]
public string description { get; set; }
}
The field "failure_type_id" is related to the business table, the other fields store the language code and the related translation.
So, after syncing datas in the SQLite database, I refresh the "translated" datas in the app and this can take a long moment. The load of the the 2 tables from the SQLite is very quickly, but the update of the "_descriptionTr" field can be very slow:
var failureType = ServiceLocator.Current.GetInstance<IRepository>().GetAll<Failure_Type>();
var failureTypeTranslations = ServiceLocator.Current.GetInstance<IRepository>().GetAll<Failure_Type_Translation>();
FailureType = new ObservableCollection<Failure_Type>();
foreach (var ft in failureType)
{
var ftt = failureTypeTranslations.FirstOrDefault(i => i.failure_type_id == ft.failure_id && i.language.ToLower().Equals(lang));
if (ftt != null)
ft._descriptionTr = ftt.description;
else
ft._descriptionTr = ft.description;
FailureType.Add(ft);
}
Is there a better way for doing this?
How could I optimize it?
Edit :
the "business" table contains 550 rows
the "translation" table contains 3500 rows
the duration of the loop is nearly 1 minute
A couple of suggestions:
Create the observable collection at once ...
FailureType = new ObservableCollection<Failure_Type>(failureType);
... so the individual additions don't fire notifications. Now use FailureType in the loop.
Instead of fetching all translations, filter them by lang:
var failureTypeTranslations = ServiceLocator.Current.GetInstance<IRepository>()
.GetAll<Failure_Type_Translation>()
.Where(l => i.language == lang);
Create a dictionary for lookup of known translations:
var dict = failureTypeTranslations.ToDictionary(ftt => ftt.failure_id);
foreach (var ft in FailureType)
{
Failure_Type_Translation ftt;
if (dict.TryGetValue(ft.failure_id, out ftt)
ft._descriptionTr = ftt.description;
else
ft._descriptionTr = ft.description;
}
I think that esp. the part failureTypeTranslations.FirstOrDefault kills performance. The query is executed for each iteration of the loop.

DataValue field value C#

I am doing a project and can't make a Seletect Value from a list get the right value.A list is generated from a class UserDepartment,in this class I have this basically:
public class UserDepartment
{
public long ID { get; set; }
public string Description { get; set; }
public User UserResponsible { get; set; }
}
The Problem is that I need a value from the class userResponsible,inside there is a value called EDV, I need this value but I don't know how to get.See this image below:
If I use " ListBeneficiaryArea.DataValueField = "ID"; ",I get the ID value normally,but i cant get the EDV value, I already tried "EDV","UserResponsible.EDV" and "UserResponsible"but it didn't work.
There is other way for me to get the UserResponsible.EDV in the DataValueField?
After the change in DataSource i received this error:
You can change your DataSource to the following:
ListBeneficiaryArea.DataSource = from a in lstBUAreas
select new { ID, EDV = a.UserResponsible.EDV };
Then you can do:
ListBeneficiaryArea.DataTextField = "ID";
ListBeneficiaryArea.DataValueField = "EDV";

C# array/list into DataGridView Column

my Code looks like this:
public class A
{
public string N{ get; set; }
public int E1 { get; set; }
public int E2{ get; set; }
public List<Genre> G{ get; set; }
[System.ComponentModel.Browsable(false)]
public string L { get; set; }
}
and I have a BindingList that contains many of them. Now I want to display this list in a DataGridView, which works pretty good, except that the List inside the A's is NOT shown.
Binding the DataSource:
dgSeen.DataSource = Storage.seen;
the List G gets set after some time, and has the correct value(s).
So all what I want is, that there is a 4th DataGridView column that shows me the content of the List like "Action, Fatasy, SciFy" and so on.
I hope you understand what I need, and thanks in advance for every help :)
You can create a computed property for this purpose.
public string GDisplay
{
get
{
return String.Join(", ", G);
}
}
Then use GDisplay in your grid to represent G. Obviously this is a read-only design. If you want to be able to modify the value of G via the grid, you'll have to implement a setter for GDisplay. For example:
set
{
G = value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Trim())
.ToList();
}

Dynamically binding and creating items with complex database structure to Create and Edit views using viewModel in MVC 3 with EF4

I had bad times trying to figure out how to deal with this. Any help wold be appreciated. Even a suggestion of a better structure that fits to my needs: Construct category items in a category having an specification of how they can be in a category item property list.
This is used to, among other things, dinamicaly generate forms for creating and editing items.
Long story short: I need to know if I'm doing it right or a better (maybe automated) way of deal with it without breaking the whole app.
.
.
I'm working with MySQL 5 in VWD Express 2010 in a Win7 64bit machine with all MySQL drivers intalled (ODBC and .NET specific provider, the last one is not compatible with ASP.Net 4). Other problem rised here, but can be target for a separate question: I'm writing all of my models, 'cause MySql isn't compatible with the Linq to SQL (I can imagine why, but not sure).
.
.
Returning to the real topic:
My models are:
Category - Them main entity, with a name property, a collection of CategoryItemProperty entities and a collection of Item entities;
CategoryItemProperty - An entity with a name and some other properties that dictate how the Items in this category may be (field size, mask, input restriction, etc);
Item - The entity whose properties are based on the category properties;
ItemProperty - The properties of the items (field size, mask, input restriction, etc)
The code is something around this:
public class Category
{
public int CategoryId { get; set }
public string Description { get; set }
//...
public virtual List<CategoryItemProperty> ItemProperties { get; set; }
}
public class CategoryItemProperty
{
public int CategoryItemPropertyId { get; set; }
public string Label { get; set; }
public string Name { get; set; }
public int Size { get; set; }
public int MaxLenght { get; set; }
//...
public virtual Category Category { get; set; }
}
public class Item
{
public int ItemId { get; set; }
public string Description { get; set; }
public int CategoryId { get; set; }
//...
public virtual Category Category { get; set }
public virtual List<ItemProperty> Properties { get; set; }
}
public class ItemProperty
{
public int ItemPropertyId { get; set; }
public int ItemId { get; set; }
public int CategoryItemPropertyId { get; set; }
public string Value { get; set; }
//...
public virtual Item Item { get; set; }
public virtual CategoryItemProperty CategoryItemProperty { get; set; }
}
.
.
The big problem here, with this approach, is to generate the form and deal with the data on the controller side to be saved to the database.
.
.
A more detailed example wold be: Generate a simple contact form:
We create a Category with some field specification:
var category = new Category() { Description = "Simple Contact Form" };
MyEntitySet.Categories.Add(category);
MyEntitySet.SaveChanges();
//...
var name = new CategoryItemProperty() { Label = "Name", Size = 50, MaxLength = 50 };
var message = new CategoryItemProperty() { Label = "Message", Size = 50, MaxLength = 255 };
category.ItemProperties.Add(name);
category.ItemProperties.Add(message);
MyEntitySet.Entry(category).State = EntityState.Modified;
MyEntitySet.SaveChanges();
.
.
What I have came up until now is to create a ViewModel to pass the category info and its item property collection to the Create and Edit views and doing a loop through the ItemProperties to generate the fields and working in the ItemController, receiving the FormCollection and generating the Item and its ItemPropertys objects and saving them to the database. But this process is terrible and painfull:
.
Items/Create View:
#model MyApp.Models.CategoryItemModelView
#Html.EditorFor(m => m.Description);
...
#foreach(var property in Model.ItemProperties)
{
<label>#property.Label</label>
<input type="text" name="#property.Name" size="#item.Size" maxlength="#item.MaxLength" />
}
In the Controller:
// GET: /Items/Create/5
public ActionResult Create(int id)
{
var categoryItemModelView = new CategoryItemModelView();
categoryItemModelView.Populate(id); // this method maps the category POCO to the ViewModel
return View(categoryItemModelView);
}
// POST: /Items/Create/5
[HttpPost]
public ActionResult Create(int id, FormCollection formData)
{
if (ModelState.IsValid)
{
var category = MyEntitySet.Categories.Find(id);
var item = new Item
{
CategoryId = id,
Description = formData["Description"],
// ...
Category = category
};
MyEntitySet.Items.Add(item);
foreach(var property in category.ItemProperties)
{
var itemProperty = new ItemProperty
{
ItemId = item.ItemId,
CategoryItemPropertyId = property.Id,
Value = form[property.Name],
// ...
Item = item,
CategoryItemProperty = property
};
MyEntitySet.ItemProperties.Add(itemProperty);
}
MyEntitySet.SaveChanges();
return RedirectToAction("Index");
}
// Here I don't know how to return the typed data to the user (the form returns empty)
var categoryItemModelView = new CategoryItemModelView(id);
categoryItemModelView.Populate(id); // this method maps the category POCO to the ViewModel
return View(categoryItemModelView);
}
.
.
My problem rises in building the Create and Edit actions and its respective views (see above how I'm doing it right now). How to handle this case, when I have to use the Category.ItemProperties to generate the fields and store the information in an Item object and the field values in its ItemProperty object?
.
.
Please note: All this code is for example purposes only. My code is similar, but its handled by a specific controller and specific views to CRUD Categories and CategoryItemProperties and I have no problem with this.
.
.
Sorry for this long text. I've tryed to be as clearest as I can. If you need any more info, drop a comment, please.
Okay rcdmk! first of all my English is terrible and i'm here to just share with you my few experience.
I have build such a complex software in the past with MVVM(WPF) + EF4 + T4 to generate POCO's and i deal with Microsoft Blend to handle with actions, bindings and so on between the client and the viewmodels!
that's work great! i hope i helped you!
ps:
I dont know if Blend supports ASP.Net but creating POCO(Viewmodel) with lazy loading could help u somewhere!
As i understand , Category and corresponding CategoryPropertyItems is describing how Item will be created . Simply Category is drawing an abstract form and item and item properties are concretes (because item property has Value property). So in Item/Create Action (GET) you can build item and it's properties using Category and CategoryPropertyItems.
public Item Build(Category category)
{
Item item = new Item();
item.Category = category;
item.ItemId = ...
foreach(var categoryItemProperty in category.ItemProperties)
{
ItemProperty itemProperty = new ItemProperty();
itemProperty.Item = item;
itemProperty.CategoryItemProperty = categoryItemProperty;
itemProperty.ItemPropertyId = ...
}
return item;
}
In result of Index/Create Action you can use either just this Item object or you can put item into ViewModel .
In View you can bind direcly to model (Item) properties .
This links can help you .
Editing and binding nested lists with ASP.NET MVC 2
Model Binding To A List

Categories