Convert SQL (LEFT OUTER JOIN) to LinQ [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Could someone please convert the below raw sql to linq ?
_sql= " SELECT tbl_book.*, tbl_time.tt_scope, tbl_time.tt_time "+
" FROM tbl_time "+
" LEFT OUTER JOIN tbl_book ON tbl_time.tt_time = tbl_book.book_time "+
" AND (tbl_book.ppt_id=#ppt_id AND tbl_book.fct_id=#fct_id "+
" AND tbl_book.book_date=#book_date)";

if
var ppt_id_param = "#ppt_id";
var fct_id_param = "#fct_id";
var book_date_param = "#book_date";
be our params, and your context be this:
TestEntities DBContext = new TestEntities();
your linq query is like:
var query = (from time in tbl_time
join book in tbl_book on time.tt_time equals book.book_time
into joinedList
from book in joinedList.DefaultIfEmpty()
where book == null ?
true :
(
book.ppt_id == ppt_id_param &&
book.fct_id == fct_id_param &&
book.book_date == book_date_param &&
)
select new {
time.tt_scope,
time.tt_time
book
})
.ToList();

Related

In the rust test, I don't care about the sequence of vec![]. What should i do? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
enter image description here
Is there any disorderd arrays Or Do not judge the order when asserting?
What I do is sort both sides in the assert call itself. This only works if T implements Ord.
let result = my_function();
my_function.sort();
let target = vec![];
target.sort();
assert_eq!(result, target);
If your datatype does not support Ord, you can use sort_by with a FnMut that returns an instance of Ordering.
Note that this can have issues when there isn't one specific way a vector can be sorted.
Convert the Vec(s) to HashBag(s) that contains references to the items in the Vecs. That will disregard the order of items when asserting for equality:
[dependencies]
hashbag = "0.1.9"
#[test]
fn two_vecs_equal_independent_of_item_order() {
use hashbag::HashBag;
let actual = vec![1, 2, 3, 3];
let expected_fail = vec![3, 2, 1];
assert_ne!(
actual.iter().collect::<HashBag<&i32>>(),
expected_fail.iter().collect::<HashBag<&i32>>()
);
let expected_pass = vec![3, 2, 1, 3];
assert_eq!(
actual.iter().collect::<HashBag<&i32>>(),
expected_pass.iter().collect::<HashBag<&i32>>()
);
}

Kotlin - how to use sort function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
Hallo Ladys and Gentleman,
i strugle with a function.
I want convert a given String to a MutableMap.
String:
var testString = "hshhfzrt" + "hszrhhtnt"
function to use:
charMap.toList().sortedByDescending { (_, value) ->
value }.toMap()
Final Output should be a Sorted Char in a new String.
Output example(Not related to testString is just example):
hhh, lll, aaa,
I hope you can help me.
Thx for reading this and your time.
val testString = "hshhfzrt" + "hszrhhtnt"
val result = testString
.toList()
.sorted()
.groupBy { it }
.map { it.value.joinToString("") }
.joinToString(", ")
println(result) // Output: "f, hhhhhh, n, rr, ss, ttt, zz"

Kotlin compare two lists - if ids match overwrite it [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have two lists of Object. Both objects have an id val. I need to check both lists by the id val, if the id is the same take the object from list B and overwrite it in list A. Is there a simple up to date way to achieve this outcome in kotlin?
Ive been searching through the kotlin docs and other comparing list questions on here but I havent found anything in the docs or on here that matches my usecase
Not sure about how efficient this is but it works...
Sample Class
data class SomeClass(val id: Int, val someString: String) {
}
fun transformList(firstList: List<SomeClass>, secondList: List<SomeClass>): List<SomeClass> {
return firstList.map { firstClass ->
val tempItem = secondList.firstOrNull { it.id == firstClass.id }
tempItem ?: firstClass
}
}
basically, the function takes both the lists and compares each item with each other, and returns items from list 2 if the id is the same.
fun main() {
val listOne = listOf<SomeClass>(
SomeClass(0, "I am 0, from list 1"),
SomeClass(1, "I am 1, from list 1"),
)
val listTwo = listOf<SomeClass>(
SomeClass(0, "I am 0, from list 2"),
SomeClass(1, "I am 1, from list 2"),
)
println(transformList(listOne, listTwo))
}
Output
[SomeClass(id=0, someString=I am 0, from list 2), SomeClass(id=1, someString=I am 1, from list 2)]

How to check array integer value in kotlin? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
i want to make check value function like below code.
enabled is true only if the integer array contains 5 or 16, otherwise enabled is false.
Anyone can help?
Use the default IntArray.any(predicate: (Int) -> Boolean) : Boolean function to check it. Example:
#Test
fun intArray_containsElement() {
val arrayTest = intArrayOf(1, 10, 50, 5, 6, 4, 3)
val isEnable = arrayTest.any { it == 5 || it == 16 }
assertEquals(true, isEnable) // Successfully
}
edit: As Duc Thang pointed out, using any is better because it only iterates the array once:
val IntArray.enabled: Boolean get() = any { it == 5 || it == 16 }
Previous less efficient version:
val IntArray.enabled: Boolean get() = contains(5) || contains(16)
Test:
fun main() {
println(intArrayOf(0).enabled)
println(intArrayOf(0, 5).enabled)
println(intArrayOf(1, 16).enabled)
println(intArrayOf(5, 16).enabled)
println(intArrayOf(19, 1000, 4).enabled)
}
Output:
false
true
true
true
false

JSON Array goes crazy [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a huge problem with my two lovely JSON-Arrays.
The code is like:
else if ($range == "day") $sqlRangeString = "GROUP BY DATE_FORMAT(dbtime, '%Y-%c-%e')";
$startdate = doTimeStamp($startdate);
$enddate = doTimeStamp($enddate);
if (isset($consumer_on))
{
echo '[';
$counter = 0;
foreach($consumer_name as $consumer_name_value)
{
$result2 = mysql_query("SELECT AVG(loadvalue) AS m1, dbtime, time
FROM $consumer_name_value
WHERE time >= $startdate
AND time <= $enddate
$sqlRangeString
ORDER BY time") or die('#ä');
while ($data2 = mysql_fetch_array($result2))
{
$consumer_value[$counter][0] = $data2['time'];
if (!isset($consumer_value[$counter][1])) $consumer_value[$counter][1] = 0;
$consumer_value[$counter][1] = (float)$consumer_value[$counter][1] + (float)$data2['m1'];
$counter++;
}
}
echo json_encode($consumer_value);
}
if (isset($producer_on))
{
$ounter = 0;
if (isset($consumer_on) && ($consumer_on == 1))echo ',';
foreach($producer_name as $producer_name_value)
{
$result3 = mysql_query("SELECT AVG(power) AS m1, dbtime, time
FROM $producer_name_value
WHERE time >= $startdate
AND time <= $enddate
$sqlRangeString
ORDER BY time") or die('#ää'); ;
while ($data3 = mysql_fetch_array($result3))
{
$producer_value[$counter][0] = $data3['time'];
if (!isset($producer_value[$counter][1])) $producer_value[$counter][1] = 0;
$producer_value[$counter][1] = $producer_value[$counter][1] + (float)$data3['m1'];
$counter++;
}
}
echo json_encode($producer_value);
}
}
?>
My problem is the output :
Why does the output from the produver_value array has the number in front of each part? How can I remove this?
The "array" has a number before each item because the PHP array indexes don't start from 0. You have an associative array instead of a regular one, which makes json_encode produce a JSON object instead of an array.
It's possible that this happens because of this typo:
$ounter = 0; // should be $counter ?
You can use array_values to extract the values from the array.