Linq Collection instersection List/Array - entity-framework

Consider the following list:
List<long> listOfIDs = new List<long> { 1, 2, 3, 4 };
[(Product) 1 Pineapple - (Supplier) Fruit Inc / Marketplace Inc]>
[2 Strawberry - Fruit Inc]>
[3 Coke - Super Drinks Inc / Marketplace Inc]>
[4 Orange Juice - Super Drinks Inc]
db.Table.Where(a => a.SubTable.All(b => listOfIds.Contains(b.SubTableId)))
While I've selected Products 1 and 2, I should get only Fruit Inc as a Supplier. When I include Coke into my list, I don't want to see any supplier anymore because there is no Supplier that represents these 3 products at the same time.
Expected Outputs Scenarios:
Selected Products: 1, 2
Expected Result: Fruit Inc
1, 3
Marketplace Inc
1, 2, 3
Empty.
1, 3, 4
Empty.
3, 4
Super Drinks Inc

// change All to Select if you want list of each bool
db.Table.All(a => listOfIds.Contains(a.SubTableId));
This should do the trick if you want to check if listOfIds exists in the db since you mentioned ContainsAll.
If you want to get the entities from db with listOfIds then this should do it.
db.Table.Where(a => listOfIds.Contains(a.SubTableId)).ToList;

Related

Python pandas: how to unpack the statsmodel results and create a column in group by dataframe

I am trying to run linear regressions by group and add the results to a new column in the dataframe.
Here is what I'm trying to do.
df2 = pd.DataFrame.from_dict({'case': ['foo', 'foo', 'foo', 'bar', 'bar'],
'cluster': [1, 1, 1, 1, 1],
'conf': [1, 2, 3, 1, 4],
'conf_1': [11, 12, 13, 11, 14]})
def ols_res(df, xcols, ycol):
results = sm.OLS(df[ycol], sm.add_constant(df[xcols])).fit()
return results.get_influence().cooks_distance[0]
df3 = df2.groupby(['case', 'cluster'])
df3.apply(ols_res, xcols='conf', ycol='conf_1')
output I got is :
case cluster
bar 1 [nan, nan]
foo 1 [0.42857142857143005, 0.09642857142857146, 10....
dtype: object
The size of results for each group is same as number of rows in the group.
I need the above output in following format. Can some one please help me?
case cluster conf conf_1 result
0 foo 1 1 11 0.42857142857143005
1 foo 1 2 12 0.09642857142857146
2 foo 1 3 13 10....
4 bar 1 1 11 nan
5 bar 1 4 14 nan
following worked for me.
def ols_res_mod(df, xcols, ycol):
results = sm.OLS(df[ycol], sm.add_constant(df[xcols])).fit()
results.get_influence().cooks_distance[0]
print(df)
df['distance'] = results.get_influence().cooks_distance[0]
return df
not sure, whether this is an efficient way or not.

struggling to handle deduplication after aggregation in spark streaming

1.streaming data is coming from kafka
2.consuming through spark streaming
3.firstname,lastname,userid and membername ( using member names i am getting the member count
e.g mark,tyson,2,chris,lisa,iwanka - so here member count is 3
somehow i have to do the count its the requirmnt . but how can i remove deduplication after aggregation . its my concern
val df2=df.select(firstname,lastname,membercount,userid)
df2.writestream.format("console").start().awaitTermination
or
df3.select("*").where("membercount >= 3").dropDuplication("userid")
// this one is not working , but i need to do the same after
count only so that in batches same user id will not come again.
only first time entry i want.
Batch-1 output
firstname lastname member-count userid
john smith 5 1
mark boucher 8 2
shawn pollock 3 3
batch-2 output
firstname lastname member-count userid
john smith 7 (prev.count 5) 1
shawn pollock 12 (prev.count 8) 3
chris jordan 6 4
// but here i want batch -2 ---------output
1.The possibilty is the john smith ,shawn pollock count will increase again in next batches ,but i dont want to show or keep in output for next batches.
i.e based on userid , i want entry for the one time only in batch output
and neglect again the same user in batch output
firstname lastname member-count userid
chris jordan 6 4
Your question is hard to read, but as I understand you want a while loop with a condition?
var a = 10;
while(a < 20){
println( "Value of a: " + a );
a = a + 1;
}
For example will print
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

Applying accumulate on subclass in drools

I am creating the object of stock and inserting stock values as below
Customer 1 has 2 stocks stocks4 and stocks5
customer2 has 1 stocks arrayListStocks1
Here I am trying to make sum of all Stocks for given session
Stocks stocks1=new Stocks("stock1", 1, 1);
Stocks stocks2=new Stocks("stock2", 2, 2);
Stocks stocks4=new Stocks("stock4", 4, 4);
Stocks stocks5=new Stocks("stock5", 5, 5);
ArrayList<Stocks> arrayListStocks1=new ArrayList<Stocks>();
arrayListStocks1.add(stocks1);
arrayListStocks1.add(stocks2);
Customer customer1= new Customer();
customer1.setCustomerName("ACC");
customer1.setStocks(arrayListStocks1);
ArrayList<Stocks> arrayListStocks2=new ArrayList<Stocks>();
arrayListStocks2.add(stocks4);
arrayListStocks2.add(stocks5);
Customer customer2= new Customer();
customer2.setCustomerName("JINDAL");
customer2.setStocks(arrayListStocks2);
kSession.insert(customer1);
kSession.insert(customer2);
rule "Rule5"
// agenda-group "promotions2"
dialect "java"
no-loop
salience 80
when
$list: List() from collect($customer:Customer($customerName:customerName,$stk:stocks))
$customer:Customer($customerName:customerName,$stk:stocks)
$stocks:Stocks() from $stk
$listStocks: List() from collect(Stocks($stockNumber:stockNumber > 1) from $stk )
$sum:Number() from accumulate (Stocks($stockNumber: stockNumber) from $stk,sum($stockNumber))
then
// System.out.println("Rule5 Stock Number- " + $stocks.getStockNumber());
// System.out.println("Rule5 SIZE - " + $listStocks.size());
System.out.println("Number - " + $sum);
end
when I run above rule I am getting below output
Number - 9.0
Number - 9.0
Number - 3.0
Number - 3.0
how to calculate sum of 2 stocks from customer1 and customer2
I am expecting the result 12
Thanks,
If all you want to do is to calculate the sum of all the stock for all the customers, then you can try something like this:
rule "Stocks Sum"
when
$n: Number() from accumulate(
Customer($stk:stocks) and
Stocks($stockNumber: stockNumber) from $stk,
sum($stockNumber)
)
then
//...
end
Hope it helps

the first column as "key" then add the rest every column's value

the first column as "key" then add the rest every column's value
in fact the source data file more that 22 columns
as following only an example:
source file(column delimiter is a space):
a 1 2 3
b 1 2 3
a 2 3 4
b 3 4 5
desired output:
a 3 5 7
b 4 6 8
val data = scala.io.Source.fromFile("/root/1.txt").getLines
data.toList
how to do next step? thx
General algorithm for solving this task:
Split each line by separator
Group lines by first column
Remove first column from each line
Transform all strings to numbers
Sum lines
Print result
With plain Scala:
val data = List("a 1 2 3", "b 1 2 3", "a 2 3 4", "b 3 4 5")
data.map(_.split(" ")) // 1
.groupBy(_.head) // 2
.mapValues(
_.map(
_.tail // 3
.map(_.toInt)) // 4
.reduce((a1, a2) => a1.zip(a2).map(tuple => tuple._1 + tuple._2))) // 5
.foreach(pair => println(s"${pair._1} ${pair._2.mkString(" ")}")) // 6

fastest way to find the union and intersection items among two list

which is the fastest way to find the union and intersection between two lists?
i mean.
i have two list say
List<1>
1
2
3
4
Lis<2>
2
3
Finally i need to get output as
List<3>
Not Defined
2
3
Not Defined
Hope i am clear with my requirement.
Please let me know if i am conusing
LINQ already has Union and Intersection. Your example is neither.
var set = new HashSet(list2)
var list3 = List1.Select(x => set.Contains(x) ? x : null).ToList();
Or you could do the following, which just gives you the intersection:
HashSet<int> list1 = new HashSet<int>() { 1, 2, 3, 4 };
HashSet<int> list2 = new HashSet<int>() { 2, 3 };
List<int> list3 = list1.Intersect(list2).ToList();
for (int i = 0; i < list3.Count; i++)
{
Console.WriteLine(list3[i]);
}
Console.ReadLine();