distinct values using LINQ [duplicate] - linq

This question already has answers here:
LINQ's Distinct() on a particular property
(23 answers)
Closed 6 years ago.
I try to get distinct values in LINQ i try this for this first i create method and then i call this method on page load and assign
regiondrop.DataSource = getregion();
regiondrop.DataSourc=DataTextField="Region"
regiondrop.DataSourc==DataTextField="RID"
private List<tab1> getregion()
{
using (T1 tee = new T1())
{
var tempList = tee.tbl1.ToList();
var list = (from ta in tempList
select new { ta.Region, ta.RID }).Select(x => new tbl1
{
Id = x.RID,
reg=x.Region
}).ToList();
return list;
}
}
Data in db like this
RID Region
1 Canada
2 UK
3 London
4 Paris
5 UK
6 Brazil
7 London
Data in drop-down like this
Canada
UK
London
Paris
UK
Brazil
London
but i want data like this
Canada
UK
London
Paris
Brazil
any solution?

You can add a GroupBy
var list = from ta in tempList
group ta by ta.Region into g
select g.FirstOrDefault();

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;

Combine Two iEnumerable Objects with ID

It must be that time of year. Totally having a brain fart.
I have two basic iEnumerable objects. Each object has two fields. In the first object I have a field with an ID and then total.
Id Total
1 23
2 16
3 59
...
In the other object it has a ID field and then Fruit Name
ID Fruit
1 Apple
2 Orange
3. Pear
I need to combine these into a new table by the ID so I get a new object with the fields
ID Total Fruit
1 23 Apple
2 16 Orange
3 59 Pear
What's the best way to go about this using LINQ?
Do a join
from o in iEobject
join f in Fruit on o.ID equals f.ID
select new {ID = o.ID, Total = o.Total, Fruit = f.Name }

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

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"));

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.

Resources