linq query to fetch data by employee wise - linq

emp_master:
empid,empname
I am having one table called as
employee_travel
travelid empid location date
1 101 abc 3/4/2014
2 102 lmn 4/4/2014
3 101 abc 5/4/2014
4 102 lmn 6/4/2014
5 101 xyz 7/4/2014
6 102 cdf 8/4/2014
now iIwant to display records employee wise like:
empid location date
101 abc --
101 abc --
101 xyz
102 lmn
102 lmn
102 cdf
I have written following query in linq:
var data = (from r in context.employee_travel
group r by new
{
r.Emp_id
} into g
select new
{
name = r.emp_master.empname,
r.date,
r.location
})
But it is giving me error on this line:
****name = r.emp_master.empname,
r.date
r.location****
Here{name is used as anonomous type in query are name of d**atatextfield of my gridview**.}
Can anyone edit my linq query to suit my needs ????
Please please please help me. I am very much new to linq so I don't know how to write this query.

from et in employee_travel
orderby et.empid
select new
{
Employee = et.Employee,
TravelRecord = et, //if you want objects
Name = et.Employee.Name,
Date = et.Date //if you want simple types
}
In that sample I chose to return the entity, but you can of course return names, dates, etc. And you can add more ordering if you need.
Or you can just get the employee_travel entity and include the employee (make sure you have the right include property name)
db.employee_travel.Include("employee").OrderBy(et=>et.empid).ToList()

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;

How to select linq entities?

Database name 1: BOOK, table name: BookInformation (BookID(PK),BookName))
BookID BookName
---------------------
19 A
25 T
56 F
45 H
77 K
53 M
76 YT
I want to get the ID values ​​in the table.So I write this linq entities query.But this query error.
var query= from a in Book.BookInformation.AsEnumerable.Select(a=>a.BookID).ToList();
How to write get the ID values?
You can try this.
var idList= Book.BookInformation.Select(a=>a.BookID).ToList();

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

How can I use LINQ to find the Intersect between two properties of the same collection?

Given an IList<Foo> with a data set that looks like this:
ID CHILD PARENT TYPE
1 102 101 UPSELL
1 103 101 UPSELL
2 102 101 BONUS
2 104 103 BONUS
3 102 101 BONUS
4 102 101 PRODUCT
4 104 102 PRODUCT
How can I use LINQ to find a child which has a parent with the same ID?
Desired output is
ID CHILD PARENT TYPE
4 102 101 PRODUCT
I think this is what you're looking for. I grouped by ID first so I could process each group individually, but there's probably a way to combine it into a single query.
var grouping = foos.GroupBy(f => f.ID);
foreach(var g in grouping)
{
var result = (from f1 in g from f2 in g where f1.Child == f2.Parent select f1);
if( result.Any())
{
// you have your answer
}
}

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