LINQ - Join with List - c#

I have condition where i have to join two different types of array to form a new array.
Let's see the class where condition should be created.
public class MutualFundParameter
{
public int Id { get; set; }
public string AcNo { get; set; }
public string CustomerName { get; set; }
}
public class VoucherDetails
{
public int Id { get; set; }
public int AcNo { get; set; }
public decimal Amount { get; set; }
public int MasterId { get; set; }
}
public class VoucherMaster
{
public int Id { get; set; }
public DateTime DateTime { get; set; }
public ICollection<VoucherDetails> VoucherDetails { get; set; }
}
New let's add some data into that object
// mutual fund parameter
var mutualFundParameters = new List<MutualFundParameter>
{
new MutualFundParameter { Id = 1, AcNo = "001", CustomerName = "Ram" },
new MutualFundParameter { Id = 2, AcNo = "002", CustomerName = "Shyam" },
new MutualFundParameter { Id = 3, AcNo = "003", CustomerName = "Hari" },
new MutualFundParameter { Id = 4, AcNo = "004", CustomerName = "Gita" },
new MutualFundParameter { Id = 5, AcNo = "005", CustomerName = "Sita" },
};
// voucher details
var voucherDetails = new List<VoucherDetails>
{
new VoucherDetails { Id = 1, AcNo = 001, Amount = 10.00M, MasterId = 1 },
new VoucherDetails { Id = 2, AcNo = 001, Amount = 120.00M, MasterId = 1 },
new VoucherDetails { Id = 3, AcNo = 002, Amount = 20.00M, MasterId = 1 },
new VoucherDetails { Id = 4, AcNo = 002, Amount = 120.00M, MasterId = 1 },
new VoucherDetails { Id = 5, AcNo = 002, Amount = 30.00M, MasterId = 1 },
new VoucherDetails { Id = 6, AcNo = 002, Amount = 20.00M, MasterId = 1 },
new VoucherDetails { Id = 7, AcNo = 003, Amount = 20.00M, MasterId = 1 },
new VoucherDetails { Id = 8, AcNo = 003, Amount = 20.00M, MasterId = 1 },
new VoucherDetails { Id = 9, AcNo = 003, Amount = 20.00M, MasterId = 1 },
new VoucherDetails { Id = 10, AcNo = 004, Amount = 10.00M, MasterId = 2 },
new VoucherDetails { Id = 11, AcNo = 005, Amount = 20.00M, MasterId = 2 },
new VoucherDetails { Id = 12, AcNo = 006, Amount = 30.00M, MasterId = 2 },
new VoucherDetails { Id = 13, AcNo = 007, Amount = 40.00M, MasterId = 2 },
new VoucherDetails { Id = 14, AcNo = 008, Amount = 50.00M, MasterId = 2 },
new VoucherDetails { Id = 15, AcNo = 009, Amount = 60.00M, MasterId = 2 },
new VoucherDetails { Id = 16, AcNo = 001, Amount = 70.00M, MasterId = 2 },
};
// voucher
var voucherMasters = new List<VoucherMaster>
{
new VoucherMaster
{
Id = 1,
VoucherDetails = voucherDetails
.Where(x => x.MasterId == 1)
.ToList(),
DateTime = DateTime.Now }
};
Now i need to join MutualFundParameter with VoucherDetails but that is inside a list of 'Voucher Master'
To join i have tried this but this doesn't work as expected
var res = voucherMasters.Join(
mutualFundParameters,
voucher => voucher.VoucherDetails.FirstOrDefault().AcNo,
mutual => mutual.AcNo,
(voucher, mutual) => new
{
}
);
Thanks Hoping for positive response.

Assuming that AcNo is of the same type in both mutualFundParameter and voucherDetail, here are two examples querying either in-memory collection or db:
(Note: I've not compiled/run the code so please excuse any syntactical errors)
var resultFromInMemory = (
from vd in voucherMasters.SelectMany(x => x.voucherDetails)
join mfp in mutualFundParameters on mfp.AcNo equals vd.AcNo
select new { vd, mfp });
var resultFromDb = (
from vd in db.voucherDetails
join mfp in db.mutualFundParameters on mfp.AcNo equals vd.AcNo
select new { vd, mfp });
For in-memory collection since you're expecting voucherDetails to be a child collection of voucherMaster, performing a join between mutualFundParameters and voucherDetails requires that voucherDetails is first consolidated into a temporary single collection which .SelectMany helps with
In case of database (assuming relational db) no such intermediate collection would be needed as every entity is a flat table in there.

Related

How to get data from 3 table into 1 list json

Here is my previous post.
https://stackoverflow.com/questions/75106799/how-to-get-data-from-3-table-into-1-list/.
It works fine.
But i expect returning value as:
[{ "id": "1f5a6c7c-6168-4ac8-73a5-08daf474a373", "name": "Bag A", "quantity": 10, "category": "Cat-A" }, { "id": "9b8eb0cc-0da4-4b6a-73a6-08daf474a373", "name": "Shirt A", "quantity": 10, "category": "Cat-B" }, { "id": "DB2EE420-A4E5-407A-5F96-08DAF4759F9C", "name": "Shoes A", "quantity": 10, "category": "Cat-C" } ]
I use .net core 6 MVC - code first. Please help me.
I want to return value without "bag", "shirts","shoes". Look like
above:
Well, based on your question, you could achieve by introduing new model which would be containing all property that your "bag", "shirts","shoes" containing. Finally, you need to loop through the existing list item and bind those into new class/model. You can have a try in following way:
Create A new Model for generic List:
public class GenericClass
{
public int Id { get; set; }
public string Name { get; set; }
public int Quantity { get; set; }
public string Category { get; set; }
}
Note: We will use this model to loop over our existing list thus we would bind them into this.
Base Model:
public class GenericClass
{
public int Id { get; set; }
public string Name { get; set; }
public int Quantity { get; set; }
public string Category { get; set; }
}
public class Bags
{
public int Id { get; set; }
public string Name { get; set; }
public int Quantity { get; set; }
public string Category { get; set; }
}
public class Shirts
{
public int Id { get; set; }
public string Name { get; set; }
public int Quantity { get; set; }
public string Category { get; set; }
}
public class Shoes
{
public int Id { get; set; }
public string Name { get; set; }
public int Quantity { get; set; }
public string Category { get; set; }
}
Seed Data Into Model:
List<Bags> listBags = new List<Bags>();
listBags.Add(new Bags() { Id = 101, Name = "Bag A", Quantity = 10, Category = "Cat-A" });
listBags.Add(new Bags() { Id = 102, Name = "Bag B", Quantity = 15, Category = "Cat-A" });
listBags.Add(new Bags() { Id = 103, Name = "Bag C", Quantity = 20, Category = "Cat-A" });
List<Shirts> listShirts = new List<Shirts>();
listShirts.Add(new Shirts() { Id = 101, Name = "Shirt A", Quantity = 10, Category = "Cat-B" });
listShirts.Add(new Shirts() { Id = 102, Name = "Shirt B", Quantity = 15, Category = "Cat-B" });
listShirts.Add(new Shirts() { Id = 103, Name = "Shirt C", Quantity = 20, Category = "Cat-B" });
List<Shoes> listShoes = new List<Shoes>();
listShoes.Add(new Shoes() { Id = 101, Name = "Shirt A", Quantity = 10, Category = "Cat-S" });
listShoes.Add(new Shoes() { Id = 102, Name = "Shirt B", Quantity = 15, Category = "Cat-S" });
listShoes.Add(new Shoes() { Id = 103, Name = "Shirt C", Quantity = 20, Category = "Cat-S" });
Build Custom List:
var genericClass = new List<GenericClass>();
foreach (var item in listBags)
{
var bag = new GenericClass();
bag.Id = item.Id;
bag.Name = item.Name;
bag.Quantity = item.Quantity;
bag.Category = item.Category;
genericClass.Add(bag);
}
foreach (var item in listShirts)
{
var shirt = new GenericClass();
shirt.Id = item.Id;
shirt.Name = item.Name;
shirt.Quantity = item.Quantity;
shirt.Category = item.Category;
genericClass.Add(shirt);
}
foreach (var item in listShoes)
{
var shoes = new GenericClass();
shoes.Id = item.Id;
shoes.Name = item.Name;
shoes.Quantity = item.Quantity;
shoes.Category = item.Category;
genericClass.Add(shoes);
}
Complete Demo:
[HttpGet("GetFrom3TablesWithSameKey")]
public IActionResult GetFrom3TablesWithSameKey()
{
List<Bags> listBags = new List<Bags>();
listBags.Add(new Bags() { Id = 101, Name = "Bag A", Quantity = 10, Category = "Cat-A" });
listBags.Add(new Bags() { Id = 102, Name = "Bag B", Quantity = 15, Category = "Cat-A" });
listBags.Add(new Bags() { Id = 103, Name = "Bag C", Quantity = 20, Category = "Cat-A" });
List<Shirts> listShirts = new List<Shirts>();
listShirts.Add(new Shirts() { Id = 101, Name = "Shirt A", Quantity = 10, Category = "Cat-B" });
listShirts.Add(new Shirts() { Id = 102, Name = "Shirt B", Quantity = 15, Category = "Cat-B" });
listShirts.Add(new Shirts() { Id = 103, Name = "Shirt C", Quantity = 20, Category = "Cat-B" });
List<Shoes> listShoes = new List<Shoes>();
listShoes.Add(new Shoes() { Id = 101, Name = "Shirt A", Quantity = 10, Category = "Cat-S" });
listShoes.Add(new Shoes() { Id = 102, Name = "Shirt B", Quantity = 15, Category = "Cat-S" });
listShoes.Add(new Shoes() { Id = 103, Name = "Shirt C", Quantity = 20, Category = "Cat-S" });
var genericClass = new List<GenericClass>();
foreach (var item in listBags)
{
var bag = new GenericClass();
bag.Id = item.Id;
bag.Name = item.Name;
bag.Quantity = item.Quantity;
bag.Category = item.Category;
genericClass.Add(bag);
}
foreach (var item in listShirts)
{
var shirt = new GenericClass();
shirt.Id = item.Id;
shirt.Name = item.Name;
shirt.Quantity = item.Quantity;
shirt.Category = item.Category;
genericClass.Add(shirt);
}
foreach (var item in listShoes)
{
var shoes = new GenericClass();
shoes.Id = item.Id;
shoes.Name = item.Name;
shoes.Quantity = item.Quantity;
shoes.Category = item.Category;
genericClass.Add(shoes);
}
return Ok(genericClass);
}
Output:
Note: If you still have any concern please have a look on our official documnet here.

How to seed a table with data from another table

I made a simple website in ASP.NET Core with Entity Framework where you can manage employees. You have and Employee controller, those Employees belong to a specific department, and there are groups who are assigned to tasks and every kind of employee can be on it from every department.
Here is my seeding:
modelBuilder.Entity<Employee>()
.HasData(
new Employee() { Id = -99, Name = "Mary", Email = "mary#gmail.com", DepartmentId = -99, GroupId = -1 },
new Employee() { Id = -98, Name = "Stan", Email = "stan#gmail.com", DepartmentId = -99, GroupId = -1 },
new Employee() { Id = -97, Name = "Mike", Email = "mike#gmail.com", DepartmentId = -99, GroupId = -1 });
modelBuilder.Entity<Department>()
.HasData(
new Department() { DepartmentId = -99, Field = "IT", Name = "Programming Department" },
new Department() { DepartmentId = -98, Field = "HR", Name = "Human Resorcues" },
new Department() { DepartmentId = -97, Field = "AD", Name = "Advertisement Department" });
modelBuilder.Entity<Group.Group>()
.HasData(
new Group.Group() { GroupId = -1, Name = "Cleaner", Task = "Clean the building"}
);
The thing is, I want that 1 group to have Employees in it, but I don't know how to seed those employees which are added before with Id -99,-98 and -97.
EDIT: Here I tried to make a list of my Employees and add them to the Employees field in Group:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var emp1 = new Employee() { Id = -99, Name = "Mary", Email = "mary#gmail.com", DepartmentId = -99, GroupId = -1 };
var emp2 = new Employee() { Id = -98, Name = "Stan", Email = "stan#gmail.com", DepartmentId = -99, GroupId = -1 };
var emp3 = new Employee() { Id = -97, Name = "Mike", Email = "mike#gmail.com", DepartmentId = -99, GroupId = -1 };
List<Employee> empList = new List<Employee>() { emp1, emp2, emp3 };
modelBuilder.Entity<Employee>()
.HasData(empList);
modelBuilder.Entity<Department>()
.HasData(
new Department() { DepartmentId = -99, Field = "IT", Name = "Programming Department" },
new Department() { DepartmentId = -98, Field = "HR", Name = "Human Resorcues" },
new Department() { DepartmentId = -97, Field = "AD", Name = "Advertisement Department" });
modelBuilder.Entity<Group.Group>()
.HasData(
new Group.Group() { GroupId = -1, Name = "Cleaner", Task = "Clean the building", Employees = empList }
);
}
But I get this error:
The seed entity for entity type 'Group' cannot be added because it has the navigation 'Employees' set. To seed relationships, you need to add the related entity seed to 'Employee' and specify the foreign key values {'GroupId'}. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the involved property values.
EDIT: Here is the seeding method:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Employee>()
.HasData(
new Employee() { Id = -99, Name = "Mary", Email = "mary#gmail.com", DepartmentId = -99, GroupId = -1 },
new Employee() { Id = -98, Name = "Stan", Email = "stan#gmail.com", DepartmentId = -99, GroupId = -1 },
new Employee() { Id = -97, Name = "Mike", Email = "mike#gmail.com", DepartmentId = -99, GroupId = -1 });
modelBuilder.Entity<Department>()
.HasData(
new Department() { DepartmentId = -99, Field = "IT", Name = "Programming Department" },
new Department() { DepartmentId = -98, Field = "HR", Name = "Human Resorcues" },
new Department() { DepartmentId = -97, Field = "AD", Name = "Advertisement Department" });
modelBuilder.Entity<Department>()
.HasMany(a => a.Employees);
modelBuilder.Entity<Group>()
.HasData(
new Group() { GroupId = -1, Name = "Cleaner", TodoId = -99 }
);
modelBuilder.Entity<Group>()
.HasMany(a => a.Employees);
modelBuilder.Entity<Todo>()
.HasData(
new Todo() { ProblemId = -99, Title = "Spilled drink in basement", Descrpition = "Someone spilled drink all over the place in the basement", IsDone = false, GroupId = -1 });
modelBuilder.Entity<Todo>()
.HasOne(a => a.Group);
}
Here is my Group.cs :
public class Group
{
[Key]
public int GroupId { get; set; }
[Required]
public string Name { get; set; }
public int TodoId { get; set; }
public Todo problem;
public List<Employee> Employees { get; set; } = new List<Employee>();
}
and my Employee.cs:
public class Employee
{
public int Id { get; set; }
[Required]
[MaxLength(50, ErrorMessage = "Name cannot exceed 50 characters")]
public string Name { get; set; }
[Required]
[RegularExpression(#"^[a-zA-Z0-9_0+-]+#[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$", ErrorMessage = "Invalid Email Format")]
[Display(Name = "Office Email")]
public string Email { get; set; }
public int DepartmentId { get; set; }
public Department Department { get; set; }
public int GroupId { get; set; }
public Group Group { get; set; }
}
The way I found out that nothing is in the Employees list is I logged out context.Employees.Count in the details function in the controller itself and it said 0

Reduce Time Complexity of Code with nested foreach loops

I am new to C# development, I have a "Comments" Class as below.
class Comments
{
public Id {get; set;}
public Text {get;set;}
public ParentId {get;set;}
public List<Comments> childComments {get;set;}
}
We have child comments field within main comments. I am saving each comment object as a Document in NoSQL DB. I need to fetch these all comments and convert them into single comment object by placing all of its child comments inside 'childComments' field. ParentId will be null if comment is at level 0(top most level or first level comment).
I wrote the below code to retrieve it.
List<Comments> parentcomments = <from DB>.Where(t => t.ParentId == ObjectId.Empty).ToList();
List<Comments> childcomments = <from DB>.Where(t => t.ParentId != ObjectId.Empty).ToList();
foreach(comment t in parentcomments)
{
finalCommentTree = AggregateComment(childcomments, t.Id);
}
public List<Comments> AggregateComment(List<Comments> childcomments, ObjectId parentId)
{
List<Comments> recursiveObjects = new List<Comments>();
foreach (Comments item in childcomments.Where(x => x.ParentId.Equals(t.ParentId)))
{
recursiveObjects.Add(new Comments
{
Id = item.Id,
Text = item.Text,
childComments = AggregateComment(childcomments, item.Id)
});
}
return recursiveObjects;
}
Code works good without any issues, but problem is with time complexity. Is there a way to reduce time complexity and improve performance?
Another approach:
List<Comments> parentList = new List<Comments>()
{ new Comments() { Id = 1, Text = "Parent1", ParentId = -1 },
new Comments() { Id = 2, Text = "Parent2", ParentId = -1 },
new Comments() { Id = 3, Text = "Parent3", ParentId = -1 },
};
List<Comments> childList = new List<Comments>()
{
new Comments() { Id = 91, Text = "child1", ParentId = 3 },
new Comments() { Id = 92, Text = "child2", ParentId = 2 },
new Comments() { Id = 93, Text = "child3", ParentId = 1 },
new Comments() { Id = 94, Text = "child4", ParentId = 2 },
new Comments() { Id = 95, Text = "child5", ParentId = 2 },
new Comments() { Id = 96, Text = "child6", ParentId = 1 },
new Comments() { Id = 97, Text = "child7", ParentId = 2 }
};
List<Comments> k = ( from c in childList
join p in parentList
on c.ParentId equals p.Id
group c by new
{
c.ParentId
,p.Text
} into stdGrp
select new Comments
{
Id = stdGrp.Key.ParentId,
Text = stdGrp.Key.Text,
ParentId = -1,
childComments = stdGrp.OrderBy(j => j.Id).ToList(),
}
).ToList();

How to group a table by a certain attribute then count those elements that are present in an other table (with foreign keys) in LINQ?

Let's say I have two tables (entities):
class Person
{
public int Id { get; set; } // primary key
public string City { get; set; } // the attribute to group by
}
class JoinTable
{
public int Id { get; set; } // primary key
public int Person_Id { get; set; } // foreign key referencing a Person entity/row
public int SomeOther_Id { get; set; } // foreign key referencing some other irrelevant entity/row
}
I want to group all Person entities by their "City" attribute and count how many people are referenced in the JoinTable by each city.
How do I query that in LINQ?
I'm not quite sure, what you want to acchieve. But i think something like this:
// Example Data (would be great if you could write some in your questions..)
List<Person> ps = new List<Person>()
{
new Person() { Id = 1, City = "Cologne" },
new Person() { Id = 2, City = "Cologne" },
new Person() { Id = 3, City = "Berlin" },
new Person() { Id = 4, City = "Berlin" },
};
List<JoinTable> join = new List<JoinTable>()
{
new JoinTable() { Id = 1, Person_Id = 1, SomeOther_Id = 1000 },
new JoinTable() { Id = 1, Person_Id = 1, SomeOther_Id = 2000 },
new JoinTable() { Id = 1, Person_Id = 2, SomeOther_Id = 1000 },
new JoinTable() { Id = 1, Person_Id = 2, SomeOther_Id = 2000 },
new JoinTable() { Id = 1, Person_Id = 3, SomeOther_Id = 3000 },
new JoinTable() { Id = 1, Person_Id = 3, SomeOther_Id = 4000 },
};
// Join the Table and select a new object.
var tmp = ps.Join(
join, // which table/list should be joined
o => o.Id, // key of outer list
i => i.Person_Id, // key of inner list
(o, i) => new { City = o.City, Id = i.Id, SomeOtherId = i.SomeOther_Id}); // form a new object with three properties..
// now we can group out new objects
var groupingByCity = tmp.GroupBy(g => g.City);
// let's see what we got..
foreach (var g in groupingByCity)
{
Console.WriteLine(g.Key + ": " + g.Count());
foreach (var g2 in g.GroupBy(a => a.SomeOtherId)) // And yes we can group out values again..
{
Console.WriteLine(" " + g2.Key + ": " + g2.Count());
}
}

Doing multiple joins within a LINQ statement

Can someone help me translate the following SQL query into a LINQ format.
SELECT a.ID,
a.HostID,
h.URL,
a.SourceURL,
a.TargetURL,
c.Value,
a.ExtFlag
FROM Link a
INNER JOIN Host h
ON h.ID = a.HostID
INNER JOIN Ref c
ON a.ResponseCode = c.SubType
AND c.Type = 'HTTP Status'
Many Thanks
I think it would be something like:
var result = from a in Context.DGApprovedLink
join h in Context.DGHost on a.HostID equals h.ID
join c in Context.DGConfig on a.ResponseCode equals c.SubType
where c.Type == "HTTP Status"
select new {
a.ID,
a.HostID,
h.URL,
a.SourceURL,
a.TargetURL,
c.Value,
a.ExtFlag };
Create Unit test class using MStest and copy the code
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace LinqTest.Test
{
public class Employee
{
public int EmpId { get; set; }
public string Name { get; set; }
public DateTime? DOB { get; set; }
public decimal Salary { get; set; }
public DateTime DOJ { get; set; }
public bool IsActive { get; set; }
}
public class Book
{
public int BookId { get; set; }
public string Title { get; set; }
public double Price { get; set; }
}
public class BookOrder
{
public int OrderId { get; set; }
public int EmpId { get; set; }
public int BookId { get; set; }
public int Quantity { get; set; }
}
[TestClass]
public class Linqtest
{
List<Employee> Employees;
List<Book> Books;
List<BookOrder> Orders;
[TestInitialize]
public void InitializeData()
{
Employees = new List<Employee>();
Books = new List<Book>();
Orders = new List<BookOrder>();
Employees.Add(new Employee(){EmpId = 1, Name ="Test1" , DOB = new DateTime(1980,12,15),IsActive = true,Salary = 4500});
Employees.Add(new Employee() { EmpId = 11, Name = "Test2", DOB = new DateTime(1981, 12, 15), IsActive = true, Salary = 3500 });
Employees.Add(new Employee() { EmpId = 5, Name = "Test3", DOB = new DateTime(1970, 2, 15), IsActive = true, Salary = 5500 });
Employees.Add(new Employee() { EmpId = 8, Name = "Test4", DOB = new DateTime(1978, 1, 15), IsActive = true, Salary = 7500 });
Employees.Add(new Employee() { EmpId = 9, Name = "Test5", DOB = new DateTime(1972, 2, 5), IsActive = true, Salary = 2500 });
Employees.Add(new Employee() { EmpId = 10, Name = "Test6", DOB = new DateTime(1980, 10, 8), IsActive = false, Salary = 5500 });
Employees.Add(new Employee() { EmpId = 15, Name = "Test7", DOB = new DateTime(1983, 11, 25), IsActive = true, Salary = 3500 });
Books.Add(new Book(){BookId = 2, Price = 24.99,Title = "British Food"});
Books.Add(new Book() { BookId = 5, Price = 4.99, Title = "Holidays in UK" });
Books.Add(new Book() { BookId = 7, Price = 7.99, Title = "UK Laws" });
Orders.Add(new BookOrder(){EmpId = 1,OrderId = 1,BookId = 2,Quantity = 3});
Orders.Add(new BookOrder() { EmpId = 1, OrderId = 1, BookId = 5, Quantity = 1 });
Orders.Add(new BookOrder() { EmpId = 1, OrderId = 2, BookId = 7, Quantity = 5 });
Orders.Add(new BookOrder() { EmpId = 11, OrderId = 3, BookId = 2, Quantity = 3 });
Orders.Add(new BookOrder() { EmpId = 11, OrderId = 4, BookId = 7, Quantity = 3 });
}
[TestMethod]
public void CheckEmpCount()
{
var res = Employees
.Where(e => e.EmpId > 5)
.Where(t =>t.Salary>=5000);
Assert.AreEqual(2,res.Count());
res = Employees
.Where(e => e.EmpId > 5);
Assert.AreEqual(5,res.Count());
}
[TestMethod]
public void TestGroupBy()
{
var res = from e in Employees
group e by e.Salary;
Assert.AreEqual(5,res.Count());
var res1 = Employees.GroupBy(e => e.Salary);
Assert.AreEqual(5, res1.Count());
}
[TestMethod]
public void TestJoin()
{
var res = from o in Orders
join Employee e in Employees
on o.EmpId equals e.EmpId
where o.EmpId == 11
select o;
Assert.AreEqual(2,res.Count());
}
[TestMethod]
public void TestJoinData()
{
var res = from o in Orders
join Employee e in Employees
on o.EmpId equals e.EmpId
join Book b in Books
on o.BookId equals b.BookId
orderby e.EmpId
select new {o.OrderId, e.Name, b.Title, b.Price};
Assert.AreEqual("Test1", res.First().Name);
}
}
}

Categories