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

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
}
}

Related

How to count one column when another column is distinct (Oracle BIEE)

I want to have a new column showing how many orders that I have for the shipment.
How can I do that in obiee
Example Table:
Shipment
Order
Order Quantity
125
001
3
125
002
3
125
003
3
126
004
2
126
005
2
I can't have a new column with order quantity for each shipment
Add a sum for the "Shipment" attribute to the visualization and the tool does it automatically. Or create a measure using SUM("Order Quantity" by "Shipment") which you can then use to cross-calculate

How to show subjects name from subjects table depending on subjects codes in users table in laravel

I have students table with subject codes in the fields named like subj_one, subj_two, subj_three etc. There are another table named subjects with subject's codes and name.
Sample Records from students table
ID Name Subj_one Subj_two Subj_three
1 Mohan 101 102 107
2 Sohan 245 101 147
3 Neha 101 103 247
Sample Records from subjects table
subj_code subj_name
101 Hindi
102 English
103 Physics
147 Chemistry
245 Math
Now how to show subject name while I am showing student's data from students table?
In this case, firstly, create relation for each column. (not recommended though, but, since you mentioned that you want to achieve this without changing the database)
e.g for Subj_one, in the Student::class (model)
public function subjectOne() {
return this->belongsTo('App\Subject', 'Subj_one', 'subj_code'); // 'foreign_key', 'other_key'
}
Then, when you are querying for the students, use with().
$students = Student::with('subjectOne', 'subjectTwo')->get();
Later in your blade,
#foreach($students as $student)
{{ $student->Name }} with {{ $student->subjectOne->subj_name }}
#endforeach
Please go though Eloquent Relationships docs.
You have to create a relationship Many To Many.
Students
ID Name
1 Mohan
2 Sohan
3 Neha
Subjects
SubjectId SubjectName
101 Hindi
102 English
103 Physics
147 Chemistry
245 Maths
student_subject
Id Student_id Subject_id
1 1 101
2 1 102
3 1 103
4 2 147
5 2 245

How to pivot duplicate rows to distinct columns in excel

I have an excel data where I need to transpose the duplicate rows to columns suitable for analysis. Please let me know how to do this?
For example,
My excel data looks like
id metrics date value
1 A 20190812 100
1 A 20190813 100
1 A 20190814 100
1 B 20190812 200
1 B 20190813 130
2 A 20190812 100
2 B 20190813 106
2 C 20190814 104
The result I look forward to is
id A B C date
1 100 200 null 20190812
1 100 130 null 20190813
1 100 null null 20190814
2 100 null null 20190812
2 null 106 null 20190813
2 null null 104 20190814
Try below in Pivot:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content]
Pivot =
Table.Pivot(
Source,
List.Distinct(Source[metrics]),
"metrics",
"value",
List.Sum)
in
Pivot

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

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

Resources