I have a table like this:
+----+-------+------------------+
| ID | Name | TipoContenedorID |
+----+-------+------------------+
| 1 | first | 10 |
| 2 | two | 9 |
| 3 | three | 10 |
+----+-------+------------------+
So depending of "TipoContenedorID" I get the name of another table with anonymous type like this:
var vehiculo = _pService.Listar(x => x.TipoContenedor.CatalogosID.Equals("TCO"), includeProperties: "TipoContenedor").Select(x => new
{
x.TipoContenedor.ID,
x.TipoContenedor.Nombre
});
Problem is I just want to receive value one time. Actually I get:
TipoContenedor.Nombre = firstname
TipoContenedor.Nombre= secondname
TipoContenedor.Nombre = firstname
So I'm getting the first name twice. How do I distinct that TipoContenedorID if repeated just pass it? Regards
Add .Distinct()
var vehiculo = _pService
.Listar(x => x.TipoContenedor.CatalogosID.Equals("TCO"), includeProperties: "TipoContenedor")
.Select(x => new
{
x.TipoContenedor.ID,
x.TipoContenedor.Nombre
})
.Distinct();
Related
I'm having a hard time trying to execute an update on multiple rows in a table.
I've tried this code:
PayoutEntities payoutdb = new PayoutEntities();
public void SaveStatusBatch(string batchid)
{
payout_transaction data = payoutdb.payout_transaction.Where(x => x.batchid == batchid).FirstOrDefault();
data.status = "Success";
payoutdb.SaveChanges();
}
But it only updates only a single row where the batchid is a match to what is indicated in the parameter. I'm using Èntity Framework in this.
This is how my table looks like:
|batchid|name|status|
|1 |John| |
|1 |Nick| |
|1 |Bill| |
|2 |Ara | |
I wanted to update the status of John,Nick and Bill to Success.
Do you have any suggestions on how am i supposed to do this?
The concept is to change all the data and then call the SaveChanges of the DBContext object. e.g.:
public void SaveStatusBatch(string batchid)
{
payout_transaction data = payoutdb.payout_transaction
.Where(x => x.batchid == batchid)
.ToList();
data.ForEach(a=> a.status = "Success");
payoutdb.SaveChanges();
}
verbDatabase Table
The data I'm using is the below and also in the screenshot above:
+----+------------+------------+------------+
| id | infinitive | ind pre je | ind pre tu |
+----+------------+------------+------------+
| 1 | aimer | aime | aimes |
| 2 | aller | vais | vas |
| 3 | courir | cours | cours |
| 4 | servir | sers | sers |
| 5 | mourir | meurs | meurs |
| 6 | dormir | dors | dors |
| 7 | sentir | sens | sens |
| 8 | vêtir | vêts | vêts |
| 9 | fuir | fuis | fuis |
| 10 | tenir | tiens | tiens |
+----+------------+------------+------------+
I'm new to coding and am working on a French conjugation app that tests users on their conjugation by displaying a verb in infinitive form (to verbLabel.text) and the tense required (to tenseLabel.text). The goal is for the user to input (to userInput.text)the conjugated form of that verb and press a button (checkAnswer) to check their answer. If the answer is correct, a new verb will be generated, etc.
I have a CoreData entity named verbDatabase with attributes named id, infinitive, indPreJe and indPreTu.
I've managed to generate a random row (to display a random verb) but I'm having trouble generating a random attribute (either indPreJe or indPreTu). To generate a random attribute and because attribute names must begin with a lowercase letter, I cannot use the same method as I did to generate a random row.
To solve this problem, I tried creating an array which holds attribute names and then generate a random number that then appends the attribute name to the fetch request that is displayed in tenseLabel.text but tenseLabel.text displays the string "verbDatabase[0].indPreJe" instead of the value of verbDatabase[0].indPreJe.
Is there any way to get the text label to display the value after it has been appended with the random attribute name instead of the string points to that value?
If I'm tackling my data in an inefficient way, is there a better way of storing my data into Core Data that will allow me to better access it with what I'm trying to do? I'm still trying to wrap my head around the Core Data structure. I've also considered saving the verbs as separate dictionaries with the tenses as their keys and conjugated forms as their corresponding values.
Ideally I would like to be able to allow users to select which tenses they would like to be tested on and also categorising the verbs - regular form, irregular form, common verbs, etc.
Thanks in advance for any help!
#IBOutlet weak var verbLabel: UILabel!
#IBOutlet weak var tenseLabel: UILabel!
#IBOutlet weak var userInput: UITextField!
#IBOutlet weak var textView: UITextView!
var currentRow: Int = 0
var countTenseColumns: Int = 2
var currentColumn: Int = 0
var columnNames = ["id","infinitive","indPreJe","indPreTu"]
#IBAction func checkAnswer(_ sender: Any) {
if userInput.text == verbDatabase[currentRow].indPreTu {
textView.text = "correct!"
viewWillLayoutSubviews()
userInput.text = nil
}
else {
textView.text = "the correct answer is \(verbDatabase[currentRow].indPreTu ?? "error")"
}
}
func randomNumberGeneratorRow() -> Int {
//generates random number from 1 to verbDatabase.count
return Int(arc4random_uniform(UInt32(verbDatabase.count)+1))
}
func randomNumberGeneratorColumn() -> Int {
//generates random number from 2 to number of columns with tenses
return Int(arc4random_uniform(UInt32(countTenseColumns)+2))
}
override func viewWillLayoutSubviews() {
currentRow = self.randomNumberGeneratorRow()
currentColumn = self.randomNumberGeneratorColumn()
var columnName = "verbDatabase[0].\(columnNames[currentColumn])"
verbLabel.text = verbDatabase[currentRow].infinitive
tenseLabel.text = columnName
}
Is there any way to get the text label to display the value after it has been appended with the random attribute name instead of the string points to that value?
Yes, you can use value(forKey:) (see the documentation here):
var columnName = columnNames[currentColumn]
tenseLabel.text = verbDatabase[currentRow].value(forKey:columnName)
How would I write a changelog.groovy for the grails database migration plugin that would insert rows into a table if a row doesn't already exist for a range of ids? For example.
cool_stuff table has
id | some_other_id |
The cool_stuff table is populated with data. Given a range of cool_stuff ids, 1 - 2000, I would like to:
Iterate through the ids, querying the cool_stuff table to see if the combination of cool_stuff id and some_other_id = 2 exists
If it doesn't exist, insert a row with the cool_stuff id and some_other_id = 2
Threre are recoreds on "cool_stuff" table already.
You need conbination of a record that "cool_stuff.id" and "some_other_id == 2"
so, do you want like following?
table of "cool_stuff"
FROM:
id | some_other_id
----|---------------
1 | 2
2 | 1
3 | 2
4 | 1
TO:
id | some_other_id
----|---------------
1 | 2
2 | 1
3 | 2
4 | 1
2 | 2
4 | 2
Is this right??
I would like to do like following if i do that.
databaseChangeLog = {
changeSet(author: "koji", id: "123456789") {
grailsChange {
change {
CoolStuff.list().findAll {
it.someOtherId != 2
}.each{
// save new instance
new CoolStuf(id: it.id, someOtherId:2).save(flush:true)
}
}
}
}
}
I'm a new guy in zend framework 2.
I'm try to work with zend db but I got a problem:
Ex: I have a table users
1 | email1 | passwod1
2 | email2 | passwod2
3 | email3 | passwod3
4 | email4 | passwod4
Now I want to get 'only' password with ID = 2 (SELECT 'password' FROM users WHERE id = 2).
How can I do it?
Try this in your model (UserTable class) :
$select = new \Zend\Db\Sql\Select('users');
$select->where(array('id'=>2));
$select->columns(array('password'));
$resultSet = $this->tableGateway->selectWith ( $select );
$yourRow = $resultSet->current();
See Reference
You can write your manual query and can execute as below, for that you have to use TableGateway(I hope you are using TableGateway for manipulating table data):
$sql="SELECT 'password' FROM users WHERE id = 2"; // Make sure your query will not raise any error.
$resultSet = $this->tableGateway->adapter->query($sql, \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
return $resultSet;
We have a MVC 4 Web application. I used miniprofiler to check how long it is taking and it shows that the render of a view with three partial views is taking a very long time. I tried moving the information in the partial views directly onto the page and that didn't make a difference in the times at all. What should I do to reduce the render time? Any tips on where I should start looking? As you can see, the render is taking 48 seconds.
**| duration | from start(ms)**
|http://localhost:61380/CaseContacts | 653.7 | +0.0
|Controller: CaseController.Contacts | 11.9 | +641.1
| Contacts view - not an ajax request | 0.3 | +651.7
| populating view model | 0.0 | +652.1
| getting list of contacts | 374.4 | +652.1
| Find: Contacts | 499.8 | +1033.6
| Render: Contacts | 48400.8 | +1535.2
**| client event |duration(ms)| from start(ms)**
|Request start | | +2.0
|Response | 1.0 | +52245.0
|Dom loading | | +52247.0
|scripts | 390.0 | +52315.0
|Dom Content Loaded Event| 29.0 | +52707.0
|Dom Interactive | | + 52707.0
|Load Event | | + 52760.0
|Dom Complete | | + 52760.0
Update:
public IQueryable<T> FindQueryable(Expression<Func<T, bool>> predicate, Func<T, Object> orderBy)
{
return this.db.GetAll<T>().Where(predicate).OrderBy(orderBy).AsQueryable<T>();
}
public class ContactRepository : Repository<USRContact>, IContactRepository<USRContact>
{
public IList<ContactNameViewModel> GetAll(int fieldOfficeID, string searchText = "")
{
if (searchText.Length > 0)
return Mapper.Map<IList<USRContact>, IList<ContactNameViewModel>>(this.FindQueryable(x => (x.FieldOfficeID == fieldOfficeID) &&
(x.FormalName.Contains(searchText) || x.PreferredName.Contains(searchText) || x.Alias.Contains(searchText) || x.Pseudonym.Contains(searchText)), x => (x.STMGender.Gender)).ToList());
else
return Mapper.Map<IList<USRContact>, IList<ContactNameViewModel>>(this.FindQueryable(x => (x.FieldOfficeID == fieldOfficeID)).ToList());
}
Then my controller is loading the results onto a viewmodel and then:
#Html.Partial("~/Views/Shared/Contacts/ContactNameList.cshtml", Model.Contacts)
The partial view contains the following code:
#model IList<Casenator.Web.Models.Contact.ContactNameViewModel>
<ul id="NameList" style="margin-left:0">
#foreach (var item in Model)
{
#*<li>#Html.RouteLink(#item.FullName, "Contacts", new { id = item.ContactID })</li>*#
<li>#Ajax.RouteLink(item.FullName, "Contacts", new { id = item.ContactID }, new AjaxOptions { UpdateTargetId = "BasicInfo", OnSuccess="InitializeComboBoxes", OnBegin="LoadContact(" + item.ContactID + ")" }) </li>
}
</ul>
Any help will be appreciated!
Thanks,
Safris
What you are returning from query is important. If you are returning IEnumerable It would be better to reurn IQueryable. You may see this question it may help you in your situation
I don't think automapper like tools are designed to map many recoreds. you may try to map the object manually