Simple.Data Distinct with InMemoryAdapter - simple.data

I'm using Simple.Data and its InMemoryAdapter to write some tests.
The code below is from one of my tests. The test should result in 1 value "Atlanta" being returned, however I get Atlanta twice. I'm wondering if I'm using InMemoryAdapter of Distinct incorrectly?
var adapter = new InMemoryAdapter();
Database.UseMockAdapter(adapter);
var db = Database.Open();
db.ParentRegionList.Insert(Id: 1, RegionName: "Dublin");
db.ParentRegionList.Insert(Id: 2, RegionName: "Atlanta");
db.ParentRegionList.Insert(Id: 3, RegionName: "Atlanta");
db.ParentRegionList.Insert(Id: 4, RegionName: "Killarney");
db.ParentRegionList.Insert(Id: 5, RegionName: "Bournemouth");
var result = db.ParentRegionList.All()
.Where(db.ParentRegionList.RegionName.Like("At%"))
.Distinct()
.Select(db.ParentRegionList.RegionName).Take(10)
.ToScalarList<string>();
I've also tried:
var result = db.ParentRegionList.All()
.Where(db.ParentRegionList.RegionName.Like("At%"))
.Select(db.ParentRegionList.RegionName.Distinct()).Take(10)
.ToScalarList<string>();

I think the second format is correct, I had similar problem. Instead of using a cast as
ToScalarList<string>() try creating a dummy type, eg. "Region" that has a string property with name RegionName and cast as: ToList<Region>()

Related

How does one handle scope identity statements in T-SQL T?

There doesn't seem to be any way to Fake a table then have the SCOPE_IDENTITY function run on the faked table.
Take this statement (inside a stored procedure)
INSERT INTO [dbo].[SomeTable](Id1, Id2)
VALUES (#Id1, #Id2)
SET #SomeIdentity = SCOPE_IDENTITY()
INSERT INTO [dbo].[Table2](Id3)
VALUES(#SomeIdentity)
So if I try and test this block of code
I'll fake the table [SomeTable] like this
EXEC [tSQLt].[FakeTable] #TableName = N'[SomeTable]',
#SchemaName = N'dbo',
#Identity = 0,
#ComputedColumns = 1,
#Defaults = 1
EXEC [tSQLt].[FakeTable] #TableName = N'[Table2]',
#SchemaName = N'dbo',
#Identity = 0,
#ComputedColumns = 1,
#Defaults = 1
However when it comes to test the block of code with the scope identity column, the Id3 is always null.
I've attempted identity = 0 or 1 but T-SQL doesn't seem to handle this scenario..
Has anyone solved this issue?
You need to set the #identity parameter to 1 to preserve the identity column when using tSQLt.FakeTable:
EXEC [tSQLt].[FakeTable]
#TableName = N'dbo.[SomeTable]',
#Identity = 1,
#ComputedColumns = 1,
#Defaults = 1
(Note: the #SchemaName parameter is deprecated.)

Kusto create an in-memory table for testing

Just looking to create a quick in-memory/temp table for testing out queries. I've seen this done before but I'm having trouble finding any examples from a web search or StackOverflow search. I'm looking for something like this:
let TempTable = table("TestTable",
Column column1 = [1,2,3],
Column comumn2 = ["A","B","C"]
);
Result:
I don't need to save the table in any database, just want to use for testing & demonstration purposes.
you could use the datatable operator: https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/datatableoperator
for example:
let T = datatable(column1:int, column2:string)
[
1, "A",
2, "B",
3, "C",
];
... do something with T ...

Oracle SQL conditional query

I have a table as ITEM_LOC with the following format:
ITEM LOC UNIT RETAIL
100 KS 20
101 JS 22
102 RS null
I need to find the unit retail,
if the record if present for loc: RS, it should get that.
If it doesn't exist for RS, it should find for JS.
If it doesn't exist for JS, it should find for KS
and if it doesn't exist of KS, it should return NULL.
How do I go about this case ?
You could do this with a sub-select, that translates the LOC into an number ordered by priority, and which takes the best numeric value present:
SELECT *
FROM ITEM_LOC
WHERE decode(LOC, 'RS', 1, 'JS', 2, 'KS', 3, null) =
(
SELECT min(decode(LOC, 'RS', 1, 'JS', 2, 'KS', 3, 4))
FROM ITEM_LOC
)
Note that if none of these LOC codes exist, you will get an empty result set.
If there are duplicate codes, you can have more than one result.

Cassandra - CqlEngine - using collection

I want to know how I can work with collection in cqlengine
I can insert value to list but just one value so I can't append some value to my list
I want to do this:
In CQL3:
UPDATE users
SET top_places = [ 'the shire' ] + top_places WHERE user_id = 'frodo';
In CqlEngine:
connection.setup(['127.0.0.1:9160'])
TestModel.create(id=1,field1 = [2])
this code will add 2 to my list but when I insert new value it replace by old value in list.
The only help in Cqlengine :
https://cqlengine.readthedocs.org/en/latest/topics/columns.html#collection-type-columns
And I want to know that how I can Read collection field by cqlengine.
Is it an dictionary in my django project? how I can use it?!!
Please help.
Thanks
Looking at your example it's a list.
Given a table based on the Cassandra CQL documentation:
CREATE TABLE plays (
id text PRIMARY KEY,
game text,
players int,
scores list<int>
)
You have to declare model like this:
class Plays(Model):
id = columns.Text(primary_key=True)
game = columns.Text()
players = columns.Integer()
scores = columns.List(columns.Integer())
You can create a new entry like this (omitting the code how to connect):
Plays.create(id = '123-afde', game = 'quake', players = 3, scores = [1, 2, 3])
Then to update the list of scores one does:
play = Plays.objects.filter(id = '123-afde').get()
play.scores.append(20) # <- this will add a new entry at the end of the list
play.save() # <- this will propagate the update to Cassandra - don't forget it
Now if you query your data with the CQL client you should see new values:
id | game | players | scores
----------+-------+---------+---------------
123-afde | quake | 3 | [1, 2, 3, 20]
To get the values in python you can simply use an index of an array:
print "Length is %(len)s and 3rd element is %(val)d" %\
{ "len" : len(play.scores), "val": play.scores[2] }

ASP.net - Add multiple rows based on a textbox value

I’m trying to figure out the best way to add multiple rows into an existing SQL table using ASP.NET. I'm new to VBA programming so am a little lost and need some help with this problem.
Example, the user will enter.
2 (LotNo)
5 (itemNo) – Add multiple rows depending on this value.
100 (cartNo)
20120202 (Date)
Result:
2, 1, 100, 20120202
2, 2, 100, 20120202
2, 3, 100, 20120202
2, 4, 100, 20120202
2, 5, 100, 20120202
You can try with this code - based on Table Value Parameter
Dim sc As New SqlCommand(
"INSERT INTO MyNewTable (field1, field2,...)"&
"SELECT field1, field2,... FROM #MyTable;", connection)
sc.Parameters.AddWithValue("#MyTable", MyTable)
sc.ExecuteNonQuery()
Link :http://msdn.microsoft.com/en-us/library/bb510489.aspx

Resources