pass list values in where condition in linq for filtering the data in datatble - linq

I have a requirement that i am having one string[] array (which is having the id values which is , separated values) and dt as datatable.
dt is having the columns called Id, empname, designation.
now i want to filter the data from the datatable where id not in (string[] values) using linq query.
for ex:
string[] ids= [2,4,6];
dt= id empname designation
---- ------- ------------
1 robert trainer
2 thomas HRA
3 John JE
4 kapil SE
5 sachin SSE
6 Rahul Manager
now i want a linq query which return my dt as:
id empname designation
---- ------- ------------
1 robert trainer
3 John JE
5 sachin SSE

You can use LINQ To DataTable:
var result = dt.AsEnumerable()
.Where(row => !ids.Contains(row.Field<string>("Id"));

Related

PIG: Filter hive table by previous table result

I need to query one HIVE table and filter the other table with one column of the previous one.
Example:
A = LOAD 'db.table1' USING org.apache.hive.hcatalog.pig.HCatLoader();
filterA = filter A by (id=='123');
B = LOAD 'db.table2' USING org.apache.hive.hcatalog.pig.HCatLoader();
//the problem is here. filterA has many rows. I need to apply filter for each of the row.
filterB = filter B by (id==filterA.id);
Data in A:
tabid id dept location
1 1 IS SJ
2 4 CS SF
3 5 EC MD
Data in B:
tabid id name address
1 4 john 123 S AVE
2 5 jane 456 N BLVD
3 9 nick 789 GREAT LAKE DR
Expected Result:
tabid id name address
1 4 john 123 S AVE
2 5 jane 456 N BLVD
As asked in the comment, it sounds like what you're looking for is a join. Sorry if I misunderstood your question.
A = LOAD 'db.table1' USING org.apache.hive.hcatalog.pig.HCatLoader();
B = LOAD 'db.table2' USING org.apache.hive.hcatalog.pig.HCatLoader();
C = JOIN A by id, B by id;

Oracle Query Prevent Displayed Duplicate Record

Let's say i have a table structure like this :
ID | Name | SCHOOLNAME | CODESCHOOL
1 DARK Kindergarten 123 1
2 DARK Kindergarten 111 1
3 Knight NY University 3
4 Knight LA Senior HS 2
5 JOHN HARVARD 3
so, how to diplay all of the data above into like this :
ID | Name | SCHOOLNAME | CODESCHOOL
1 DARK Kindergarten 123 1
3 Knight NY University 3
5 JOHN HARVARD 3
my purpose is want to display data with the max of codeschool, but when i tried with my query below :
SELECT NAME, SCHOOLNAME, MAX(CODESCHOOL) FROM TABLE GROUP BY NAME, SCHOOLNAME
but the result is just like this :
ID | Name | SCHOOLNAME | CODESCHOOL
1 DARK Kindergarten 123 1
2 DARK Kindergarten 111 1
3 Knight NY University 3
4 Knight LA Senior HS 2
5 JOHN HARVARD 3
maybe it caused by the GROUP BY SCHOOLNAME, when i tried to not select SCHOOLNAME, the data displayed just like what i expected, but i need the SCHOOLNAME field for search condition in my query
hope you guys can help me out of this problem
any help will be appreciated
thanks
Using some wacky joins you can get a functional get max rows per category query.
What you essentially need to do is to join the table to itself and make sure that the joined values only contain the top values for the CODESCHOOL column.
I've also added a :schoolname parameter because you wanted to search by schoolname
Example:
SELECT
A.*
FROM
TABLE1 A
LEFT OUTER JOIN TABLE1 B ON B.NAME = A.NAME
AND B.CODESCHOOL < A.CODESCHOOL
WHERE
B.CODESCHOOL IS NULL AND
(
(A.SCHOOLNAME = :SCHOOLNAME AND :SCHOOLNAME IS NOT NULL) OR
(:SCHOOLNAME IS NULL)
);
this should create this output, note that dark has 2 outputs because it has 2 rows with the same code school which is the max in the dark "category"/name.
ID|NAME |SCHOOLNAME |CODESCHOOL
--| -----|----------------|----------
4|Knight|LA Senior HS | 2
5|JOHN |HARVARD | 3
2|DARK |Kindergarten 111| 1
1|DARK |Kindergarten 123| 1
It's not the most effective query but it should be more than good enough as a starting point.
Sidenote: I've been blatantly stealing this logic for a while from https://www.xaprb.com/blog/2007/03/14/how-to-find-the-max-row-per-group-in-sql-without-subqueries/
I am using an analytical window function ROW_NUMBER().
This will group (or partition) by NAME then select the top 1 CODESCHOOL in DESC order.
Select NAME,
SCHOOLNAME,
CODESCHOOL
From (
Select NAME,
SCHOOLNAME,
CODESCHOOL,
ROW_NUMBER() OVER (PARTITION BY NAME ORDER BY CODESCHOOL DESC) as rn
from myTable)
Where rn = 1;

How to query distinct column value when query all row data

I hava one MySQL Table
id name birthdate city
1 Owen 2011/01/01 USA
2 Mark 2012/05/01 UK
3 Marry 2011/01/01 JP
4 John 2011/01/01 JP
First,I uesd jqgrid to read all row data. But Now,I want to know when birthdate=2011/01/01,how many different city in the table.
Can don't used sql,only used jqgrid plugin?
You are looking for distinct function.
SELECT DISTINCT(city) FROM table WHERE birthday = "2011/01/01";

Linq query to retrieve: Customers, Orders and OrderLineItems

I have a table containing a list of customers, a second table containing orders placed by my customers and a third table containing the line items for the orders.
I would like to be able to use a Linq query to get the customer name, the number of orders and the total value of all the orders placed by this customer.
Assuming the following data:
[Customers]
CustomerId Name
---------------------
1 Bob Smith
2 Jane Doe
[Orders]
OrderId CustomerId
---------------------
1 1
2 1
3 2
[OrderLineItems]
LineItemId OrderId UnitPrice Quantity
--------------------------------------------
1 1 5 2
2 1 2 3
3 2 10 10
4 2 4 2
5 3 2 5
I would like the following result:
Name OrdersCount TotalValue
--------------------------------------------
Bob Smith 2 124
Jane Doe 1 10
What would be the Linq query to get this result?
If you presume that you have a strongly typed datacontext and a partial class Customer that you are free to add to, I would solve this problem like this:
public partial class Customer {
public int NumberOfOrders {
get { return Orders.Count(); }
}
public int TotalValue {
get { return Orders.OrderLineItems.Sum( o => o.UnitPrice * o.Quantity); }
}
}
YourDataContext db = new YourDataContext();
var query = from c in db.Customers
select new {c.Name, c.NumberOfOrders,
c.TotalValue}
This would project a new anonymous type with the data you requested.

Data returned from Oracle Proc in vb.net

I have a VB.Net function which executes a Oracle Stored Proc and returns a dataset.
Below is a list of sample records
OrderID OrderDetail Qty Date Supplier Price
1 Books 10 10-Aug-08 ABC Inc 100.00
1 Pens 20 10-Aug-08 ABC Inc 300.00
2 Keys 1 20-Aug-09 Blue cross 100.00
2 Nots 3 30-Aug-09 Blue Cross 200.00
The above records are returned as a dataset in my function.
Using the above dataset int two different functions how can I return data as shown below.
First function should return only distinct orderID records
Second function should take OrderID as input and return records based on orderID
Any suggestions?
Thanks
For the select distinct, check out Select DISTINCT on DataTable. For the other, use the DataTable.Select method. Assuming that the DataTable is the first in the DataSet ...
var dt = ds.Tables[0];
var rows = dt.Select( "OrderID = 1" );
(Sorry, I don't know any VB :)

Resources