Combine two tables using Entity Framework [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have the following query:
{
SELECT A.Id, C1.FullName AS APerson, C2.FullName As BPerson
FROM TableA AS A
LEFT JOIN TableC AS C1 ON A.FK_PersonA = C1.Id
LEFT JOIN TableC AS C2 ON A.FK_PersonB = C2.Id
UNION
SELECT B.Id, B.FullName1 AS APerson, B.FullName2 AS BPerson
FROM TableB AS B
}
I would like to convert this to an Entity Framework lambda query, is this possible?
Data Model

Firstly you should create repositories for your models. Read about this here.
So , you can use this:
var ret =
(from taRec in TableA.GetAll()
join tc1 in TableC.GetAll on taRec.FK_PersonA equals tc1.Id
into tcRecs1
from tcRec1 in tcRecs1.DefaultIfEmpty()
join tc2 in TableC.GetAll on taRec.FK_PersonB equals tc2.Id
into tcRecs2
from tcRec2 in tcRecs2.DefaultIfEmpty()
select new {
taRec.Id, APerson = tcRec1.FullName, BPerson = tcRec2.FullName
}).Union(
from tbRec in TableB.GetAll()
select new {
tbRec.Id, APerson = tbRec.FullName, BPerson = tbRec.FullName
});

Related

how to use joining with LinQ To Entity [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have this code
var value = (from dc in _context.ContractDetails
where dc.EmployeeID == id
select dc.Amount);
return value;
}
is it acceptable to do Value.Sum();
You want to return the sum it looks like. Instead of having query be a decimal, just let it be what it wants (var, it's really IEnumerable<decimal>). Then you can return an aggregate on that. Sum for example
var query = from emp in Employees
join cd in ContractDetails
on emp.EmployeeID equals cd.EmployeeID
where cd.EmployeeID == id
select cd.Amount;
return query.Sum();
If this is all it does, then I also feel like you don't need to join at all, and it would be simpler to do
var query = from cd in ContractDetails
where cd.EmployeeID == id
select cd.Amount;
return query.Sum();
... unless you were using the join to test for the existence of an employee in the Employee table as a condition.
Your linq statement results in an IQueryable<Amount>, you would need to take that result and call Sum() on it to get the result you're seeking.
First, isn't there a navigation property you can use (i.e. Employee.ContracteDetails) instead of manually joining the two sets? For example,
var sum = _context.Employee
.Where( e => e.Id == id )
.Select( e => e.ContractDetails.Sum( cd => cd.Amount ) )
.SingleOrDefault();
Second, you're not using any information you need from Employee, even your where clause references ContractDetails alone; why start your query there? Work with _context.ContractDetails instead:
var sum = _context.ContractDetails
.Where( cd => cd.EmployeeId == id )
.Sum( cd => cd.Amount );

How to write the below SQL query to LINQ query in C#? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
How to convert normal SQL to LINQ query? Is there tools that can do that?? Online Tools
Have you tried something like this (a few joins omitted for brevity):
var result = from s in TooltipsLanguage
join c in TooltipsLanguageSection on s.Id equals c.IdLanguage
join p in TooltipsSection on c.IdSection equals p.Id
join ...
select new MyDestinationObject()
{
Id = s.BusinessEntityID,
Language = s.Language,
IdLanguage = c.IdLanguage,
...
};
This meight be help you.
var temp= edbContext.TooltipsLanguage.select(
c=> new {
TooltipsLanguage.Id,
TooltipsLanguage.Language,
TooltipsLanguage.TooltipsLanguageSection.Id,
TooltipsLanguage.TooltipsLanguageSection.IdLanguage,
TooltipsLanguage.TooltipsLanguageSection.IdSection,
TooltipsLanguage.TooltipsLanguageSection.TooltipsSection.Id,
TooltipsLanguage.TooltipsLanguageSection.TooltipsSection.Section,
TooltipsLanguage.TooltipsLanguageSection.TooltipsSection.Section.TooltipsItem.Id,
TooltipsLanguage.TooltipsLanguageSection.TooltipsSection.Section.TooltipsItem.IdItem,
TooltipsLanguage.TooltipsLanguageSection.TooltipsSection.Section.TooltipsItem.TooltipsItemText.Id,
TooltipsLanguage.TooltipsLanguageSection.TooltipsSection.Section.TooltipsItem.TooltipsItemText.IdItem,
TooltipsLanguage.TooltipsLanguageSection.TooltipsSection.Section.TooltipsItem.TooltipsItemText.IdText,
TooltipsLanguage.TooltipsLanguageSection.TooltipsSection.Section.TooltipsItem.TooltipsItemText.TooltipsText.Id,
TooltipsLanguage.TooltipsLanguageSection.TooltipsSection.Section.TooltipsItem.TooltipsItemText.TooltipsText.Texts});

Can I optimize my LINQ query? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
I'm trying to create my very first database queries using LINQ. For practice I'm using the Adventure Works 2014 database from Microsoft.
DB Diagram.
One of my first goals is to create static method for DataService returning a list of all products for given vendor's name. It looks like my method returns the correct data, but I think it is very poorly optimized. Can I improve it somehow?
public static List<Product> GetProductsByVendorName(string vendorName)
{
AdventureWorksDatabaseClassDataContext dc = new AdventureWorksDatabaseClassDataContext();
int vendorID = dc.Vendors.Where(vendor => vendor.Name == vendorName).Select(vendor => vendor.BusinessEntityID).First();
IEnumerable <ProductVendor> productsVendor = dc.ProductVendors;
IEnumerable<int> selectedProductsID = from ProductVendor productVendor in productsVendor
where productVendor.BusinessEntityID == vendorID
select productVendor.ProductID;
IEnumerable<Product> products = dc.Products;
IEnumerable<Product> selectedProducts = from Product p in products
where selectedProductsID.Contains(p.ProductID)
select p;
return selectedProducts.ToList();
}
You should use joins on database side to avoid transferring data over network and loading entities into memory:
from v in dc.Vendors
join pv in dc.ProductVendors on v.BusinessEntityID equals v.BusinessEntityID
join p in dc.Products on p.ProductID equals pv.ProductID
where v.Name == vendorName
select p
Note that if you have proper setup of navigation properties, then this query can look like
dc.Vendors.Where(v => v.Name == vendorName)
.SelectMany(v => v.ProductVendors.Select(pv => pv.Product))

linq convert query syntax to method syntax [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How can I convert this query syntax to method syntax in linq:
return (from x in db.Table1
join y in db.Table1 on x.ID equals y.ID - 1
where Convert.ToInt32(y.ID) >= Convert.ToInt32(x.ID)
orderby x.Name
select x).Distinct();
Which approach is better? I like this query approach better but I was asked to work with method syntax which looks too bloated to me.
var results = db.Table1.Join
(
db.Table1,
x=>x.ID,
x=>x.ID - 1,
(x,y)=>new{OuterTable = x, xid = x.ID, yid = y.ID}
)
.Where(x=>Convert.ToInt32(x.yid ) >= Convert.ToInt32(x.xid))
.Select(x=>x.OuterTable)
.OrderBy(x=>x.Name)
.Distinct();

Is this the code the compiler generates from the following query expression? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
var result = from c1 in a1
from c2 in a1.a2
from c3 in a1.a2.a3
select new { c1.id, c2.id, c3.id };
Is this the code the compiler generates from the above query expression:
var result = a1.SelectMany(
c1 => a1.a2.SelectMany(
c2 => a1.a2.a3.Select(
c3 => new {c1,c2,c3})));
thank you
You are correct.
This is a full outer join and will contain a1.Count * a2.Count * a3.Count items, including every combination of items from the source sequences/

Categories

Resources