Linq query results to List - vb.net

I have the following....
Dim ProjectToDelete As Project2Host = dc.Project2Hosts.Single(Function(p) p.Host = tb_Host.Text)
dc.Projects2Hosts.DeleteOnSubmit(ProjectToDelete)
dc.SubmitChanges
This worked fine as there was only ever one Project associated to a Host.
The rules have now changed and there can be more than one Project per Host. I now need to create a List of ProjectsToDelete and populate that with a Linq query and then do a DeleteAllOnSubmit(ProjectsToDelete).
How do I create the list? I've beem trying .SelectMany but with no joy.
Any help appreciated.

How about:
For Each item in dc.Project2Hosts.Where(Function(p) p.Host = tb_Host.Text)
dc.Projects2Hosts.DeleteOnSubmit(item)
Next
dc.SubmitChanges()
OR:
dc.Project2Hosts.DeleteAllOnSubmit(dc.Project2Hosts.Where(Function(p) p.Host = tb_Host.Text))
dc.SubmitChanges()

Related

Build Basket Session from $_POST["sku"] & $_POST["qty"]

I'm really struggling here so any help would be greatly appreciated.
I have a list of products on a page, each product has a form which posts two fields to a basket-controller.php
can somebody please show me how to create a session called basket-items with each post creating a separate sub array? I want to be able to work with the arrays via the sku value to then add other info from the db etc. thanks
figured it out
$_SESSION['basket'][$_POST['add-to-basket-sku']]['name'] = $_POST['add-to-basket-name'];
$_SESSION['basket'][$_POST['add-to-basket-sku']]['price'] = $_POST['add-to-basket-price'];
$_SESSION['basket'][$_POST['add-to-basket-sku']]['qty'] = $_POST['add-to-basket-qty'];

WP8 SQlite Select Query

I have following code.
string DB_PATH = Path.Combine(ApplicationData.Current.LocalFolder.Path, "Questions.sqlite");
SQLiteConnection dbConn = new SQLiteConnection(DB_PATH);
dbConn.CreateTable<QuestionModel>();
// Some code which insert the records in database from webservices.
List<QuestionModel> questionsList = dbConn.Table<QuestionModel>().ToList();
System.Diagnostics.Debug.WriteLine("List Count " + dbConn.Table<QuestionModel>().ToList().Count);
The List Count gives me 0 always. But When I use WP POWER TOOLS and get the Questions.sqlite file from emulator. It gives me records in SQLite table.
I also check the query in http://sqlitestudio.pl/ on the same Questions.sqlite but that gives me records as it should.
I tried other way to get the records as following code but none them were working.
String query = "Select * from QuestionModel;";
questionsList = dbConn.Query<QuestionModel>(query);
System.Diagnostics.Debug.WriteLine("List Count " + questionsList.Count);
With your analysis seems like only the row List<QuestionModel> questionsList = dbConn.Table<QuestionModel>().ToList(); is not working.
I helped someone in SQLiteDB on WP8 a couple of days ago, here is his question : SQLite WP8 StackOverflow
If you compare his code with your code, you are missing the Model bindind :
db.GetTableInfo("QuestionModel");
And
db.Table<QuestionModel>().ToList<QuestionModel>();
Not important but use your list variable to get count in debug write to not retrieve twice the list questionsList.Count
Hope it helps.

Locate database entry based on ID from another database entry in rails

I've been digging around a little trying to figure out how I should locate the "tweet_id" in my #savedtweets table and then locate that same "tweet_id" in my #newtweets table from a controller, so far I'ved tried something like this;
CONTROLLER
#stweet = Savedtweet.find(params[:id])
#newtweet = Newtweet.where(:tweet_id => #stweet.tweet_id)
#newtweet.status = 'new'
#newtweet.save
Basically I need to change the string "saved" in my Newtweets table to "new" based on the current Savedtweet ID. I just can't figure it out. If I do the following in console;
#stweet = Savedtweet.first
#newtweet = Newtweet.where(:tweet_id => #stweet.tweet_id)
It finds the right one. I've got to be close just not there yet. :)
You could do:
Newtweet.find_by_tweet_id(#stweet.tweet_id).update_attribute(:status, 'new')
The reason your code isn't working is because Newtweet.where() returns an array of objects. It should be Newtweet.where().first, though Newtweet.find_by_tweet_id is the preferred method.

Return newly inserted row without having to Submit to database

I need to find a way to get the newly insert row, without previously having to save to the database.
Is there a way? Or I need to keep the whole collection of row in a separated array?
Is this example I adding a row to the table tblConfig, but when I look back in the table the new row is not there.
tblConfig Config = new tblConfig { ID = Guid.NewGuid(), Code ="new config code" };
CTX.tblConfig.InsertOnSubmit(Config);
var Data = from dd in CTX.tblConfig select dd;
this.dataGridView1.DataSource = Data;
After some research , I'll do the work by attaching my LINQ query to a BindingSource object, witch will help me handle with insertion update and so.
Thanks for everyone, for your help :)
Hugo
Could you add that manually like this?
this.dataGridView1.DataSource = CTX.tblConfig.Execute(MergeOption.AppendOnly);

Adding related records in LINQ

Processing an XML file with LINQ to add records into a table in a SQL Server database via a data context. As we are adding records we need to assign the parents in various other tables. Most of the time we can find the existing parent and use it but sometimes we will need to create a new parent.
Thought we could do this like this:
Dim defaultPub As publication
defaultPub = New publication With {.name = e..Value}
Dim pub = _Data.publications.Where(Function(s) s.name = e..Value).DefaultIfEmpty(defaultPub).SingleOrDefault
.publication = pub
So we are trying to find a publication that matches e..Value from our XML, but if we can't find one then we use 'defaultPub'. If it does exist though we don't want to add it again. So maybe this approach is flawed anyway even if it did work...
Anyway it's not currently working, we get this error:
Unsupported overload used for query operator 'DefaultIfEmpty'.
The overload requires a publication and is getting one (I've checked the TypeNames in quickwatch), don't know what is going on here.
What we are really looking for is something like the find_or_create from activerecord for Ruby that LINQ seems to be a copied from.
Thanks in advance,
Dave.
OK, I can just do it like this:
Dim pub = _Data.publications.Where(Function(s) s.name = e.<source>.Value).SingleOrDefault
Dim newPub As publication
If Not IsNothing(pub) AndAlso pub.name <> "" Then
.publication = pub
Else
newPub = New publication With {.name = e.<source>.Value}
.publication = newPub
End If
Thanks Val!