How to select linq entities? - linq

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

Related

What is the most efficient way to update values of a table based on a mapping from another table

I have a table including following details.
empID department location segment
1 23 55 12
2 23 11 12
3 25 11 39
I also have a mapping table like following
Field old value new value
Department 23 74
department 25 75
segment 10 24
location 11 22
So My task is to replace old values with new values. I can actually use a cursor and update departments first then segments so on and so forth . But that is time consuming and inefficient. I would like to know if there are any efficient way to do this. Which also need to support in future if we were plan to add more columns to the mapping.
cheers.
Check this if it solves the issue.
update emp set department = (select map.new_value from map where emp.department = map.old_value);
How about copying the data to a new table?
CREATE TABLE newemp AS
SELECT e.empid,
NVL(d.new_value, e.department) AS department,
NVL(l.new_value, e.location) AS location,
NVL(s.new_value, e.segment) AS segment
FROM emp e
LEFT JOIN map d ON d.field='DEPARTMENT' AND e.department = d.old_value
LEFT JOIN map l ON l.field='LOCATION' AND e.location = d.old_value
LEFT JOIN map s ON s.field='SEGMENT' AND e.segment = d.old_value
ORDER BY e.empid;
EMPID DEPARTMENT LOCATION SEGMENT
1 84 55 12
2 84 11 12
3 75 11 39
You'll need obviously three passes through the mapping table, but only one pass through the emp table.
We use a LEFT JOIN because not all values will be changed. If no new_value is found, the NVL function uses the existing value of the emp table.
You could update the original table from this new table (if the new table has a primary key):
UPDATE (SELECT empid,
e.department as old_department,
n.department as new_department,
e.location as old_location,
n.location as new_location,
e.segment as old_segment,
n.segment as new_segment
FROM emp e
JOIN newemp n USING (empid))
SET old_department = new_department,
old_location = new_location,
old_segment = new_segment
WHERE old_department != new_department
OR old_location != new_location
OR old_segment != new_segment;

linq query to fetch data by employee wise

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()

Hive: Joining two tables with different keys

I have two tables like below. Basically i want to join both of them and expected the result like below.
First 3 rows of table 2 does not have any activity id just empty.
All fields are tab separated. Category "33" is having three description as per table 2.
We need to make use of "Activity ID" to get the result for "33" category as there are 3 values for that.
could anyone tell me how to achieve this output?
TABLE: 1
Empid Category ActivityID
44126 33 TRAIN
44127 10 UFL
44128 12 TOI
44129 33 UNASSIGNED
44130 15 MICROSOFT
44131 33 BENEFITS
44132 43 BENEFITS
TABLE 2:
Category ActivityID Categdesc
10 billable
12 billable
15 Non-billable
33 TRAIN Training
33 UNASSIGNED Bench
33 BENEFITS Benefits
43 Benefits
Expected Output:
44126 33 Training
44127 10 Billable
44128 12 Billable
44129 33 Bench
44130 15 Non-billable
44131 33 Benefits
44132 43 Benefits
It's little difficult to do this Hive as there are many limitations. This is how I solved it but there could be a better way.
I named your tables as below.
Table1 = EmpActivity
Table2 = ActivityMas
The challenge comes due to the null fields in Table2. I created a view and Used UNION to combine result from two distinct queries.
Create view actView AS Select * from ActivityMas Where Activityid ='';
SELECT * From (
Select EmpActivity.EmpId, EmpActivity.Category, ActivityMas.categdesc
from EmpActivity JOIN ActivityMas
ON EmpActivity.Category = ActivityMas.Category
AND EmpActivity.ActivityId = ActivityMas.ActivityId
UNION ALL
Select EmpActivity.EmpId, EmpActivity.Category, ActView.categdesc from EmpActivity
JOIN ActView ON EmpActivity.Category = ActView.Category
)
You have to use top level SELECT clause as the UNION ALL is not directly supported from top level statements. This will run total 3 MR jobs. ANd below is the result I got.
44127 10 billable
44128 12 billable
44130 15 Non-billable
44132 43 Benefits
44131 33 Benefits
44126 33 Training
44129 33 Bench
I'm not sure if I understand your question or your data, but would this work?
select table1.empid, table1.category, table2.categdesc
from table1 join table2
on table1.activityID = table2.activityID;

Getting Distinct Values with LINQ

i need to be able to get distinct values grouped by FileID and by SentToID
this is what i have now, and it only groups by SentToID, which is missing some records.
var sentByResults = from v in ctx.vEmailSents
where v.TypeDesc.Equals("Request")
group v by v.SentTo_ID into g
select g.OrderByDescending(x => x.DateSent).FirstOrDefault() into lastV
select new
{
ClaimID = lastV.Claim_ID,
SentToID= lastV.SentTo_ID,
};
so if i have 5 records
claim id fileid sentToID
1 15 27
1 16 27
1 15 26
1 15 26
1 15 47
right now i get 3 records back, one for each unique sentToID, but i need to get 4 records back, for each unique ID within each unique fileID
I suspect you just want to group by an anonymous type:
group v by new { v.SentTo_ID, v.FileID }
Also, given that you'll never get any empty groups, you should be able to use First instead of FirstOrDefault.

linq group by First()

I have the following list
ID Counter SrvID FirstName
-- ------ ----- ---------
1 34 66M James
5 34 66M Keith
3 55 45Q Jason
2 45 75W Mike
4 33 77U Will
What I like to do is to order by ID by ascending and then get the first value of Counter, SrvID which are identical (if any).
So the output would be something like:
ID Counter SrvID FirstName
-- ------ ----- ---------
1 34 66M James
2 45 75W Mike
3 55 45Q Jason
4 33 77U Will
Note how ID of 5 is removed from the list as Counter and SrvID was identical to what I had for ID 1 but as ID 1 came first I removed 5.
I tried the following but not working:
var query = from record in list1
group record by new {record.Counter, record.SrvID }
into g
let winner = (from groupedItem in g
order by groupedItem.ID
select groupedItem ).First()
select winner;
I get the followng message:
The method 'First' can only be used as a final query operation.
The funny thing is the full error message is:
"NotSupportedException: The method 'First' can only be used as a final query operation. Consider using the method 'FirstOrDefault' in this instance instead."
I have had a problem with using First in Entity Framework, have you tried changing to FirstOrDefault ?

Resources