Translating between LINQ formats - c#

Does anyone know how to write this
var q = from c in customers
join o in orders on c.Key equals o.Key
select new {c.Name, o.OrderNumber};
In this syntax style?
var 1= customers.
.join(???)
.select(???)
I've been googling for a way to do this for days now with now luck. Everyone preferes the first syntax for tutorials, but I find the second much easier to determine order of operation when reading.

The compiler translation process involves using a "transparent identifier" that makes the current customer and order available to the Select method. You can emulate this by making your own:
customers.Join(orders, c => c.Key, o => o.Key, (c, o) => new { c, o })
.Select(x => new { x.c.Name, x.o.OrderNumber });
Instead, you could just move your Name/OrderNumber projection up into the Join call:
customers.Join(orders, c => c.Key, o => o.Key, (c, o) => new { c.Name, o.OrderNumber });

This just takes a single call to Enumerable.Join:
var q = customers.Join(
orders,
c => c.Key,
o => o.Key,
(c, o) => new {c.Name, o.OrderNumber}
);

You might also check out LinqPad . It has a small lambda button for the bottom half of the screen, and it translates the linq query to chain methods :
from p in PropertyListings
from rl in p.ResidentialListings
select new {p.YearBuilt,p.ListingUrl}
got translated to :
PropertyListings
.SelectMany (
p => p.ResidentialListings,
(p, rl) =>
new
{
YearBuilt = p.YearBuilt,
ListingUrl = p.ListingUrl
}
)

Related

Convert Join to GROUP JOIN while using Lambda expression in linq c#

I am trying to change a join to LEFT outer join but getting all sorts of conversion errors. Below is my current join, can anybody provide any suggestion on how to do this without changing the actual logic of this join?
BRAND_NAME_MAP_MASTER objBrandNameMap = DB.PFC_MAP_MASTERs.Join(
DB.BRAND_NAME_MAPs,
a => a.BRAND_NAME_MAP_ID, b => b.BRAND_NAME_MAP_ID,
(a, b) => new { a, b }).Where(x => x.a.BRAND_NAME_MAP_ID == BrandNameMapID &&
x.b.BRAND_NAME_MAP_ID == BrandNameMapID).Select(x => x.a).FirstOrDefault();
Since you are only keeping a in the end changing to a left join implies not caring whether b matches or not, so the result is just:
BRAND_NAME_MAP_MASTER objBrandNameMap = DB.PFC_MAP_MASTERs
.Where(a => a.BRAND_NAME_MAP_ID == BrandNameMapID)
.FirstOrDefault();
You should use NetMage's answer if thats your entire query. But, if you still need to do a left outer join then use this:
BRAND_NAME_MAP_MASTER objBrandNameMap = DB.PFC_MAP_MASTERs.GroupJoin(
DB.BRAND_NAME_MAPs,
a => a.BRAND_NAME_MAP_ID,
b => b.BRAND_NAME_MAP_ID,
(a, b) => new { a, b })
.SelectMany(
x => x.b.DefaultIfEmpty(),
(x,y) => new { x.a, y})
.Where(x => x.a.BRAND_NAME_MAP_ID == BrandNameMapID)
.Select(x => x.a).FirstOrDefault();
Your intent doesn't make sense since you are only fetching a. If you only want a then there is no need of left join. Though i have written the query which will give you left join result. You can use it the way you want.
BRAND_NAME_MAP_MASTER objBrandNameMap =
(from Master in DB.PFC_MAP_MASTERs.Where(x=>x.BRAND_NAME_MAP_ID ==BrandNameMapID)
join Map in DB.BRAND_NAME_MAPs.Where(z=>z.BRAND_NAME_MAP_ID ==BrandNameMapID)
on Master.BRAND_NAME_MAP_ID equals Map.BRAND_NAME_MAP_ID
into result
from res in result.DefaultIfEmpty()
select new {Master,res}).ToLisT()
Hope it helps.

How to nested parsing expression .NET C#

How to nested parsing expression ,I'm developing an ORM, who can help me with some ideas?
var list = db.Queryable<Student>()
.Where(it => it.Id.Equals(db.Queryable<School>()
.Where(sc => sc.Id == it.Id&&sc.Name=="jack"))).ToList()
It's batter to use LINQ joins for this purpose.
Example:
1| LINQ query-syntax
var list = (from s in db.Student
join sc in db.School on s.SchoolId equals sc.Id
where sc.Name=="jack"
select s).ToList() ;
2| LINQ Extension method
var list = db.Student.Join(db.School,
s => s.SchoolId,
sc => sc.Id,
(s, sc) => new { s, sc })
.Where(ss => ss.sc.Name=="jack")
.Select(ss => ss.s).ToList();
You can get more information about query-syntax and Extension method in this answer.

Left join with group by linq lambda expression c#?

I want to write left join using linq lambda expression. I have tried query using join but now I want to create using left join so any one can help me how can do.
Here this is my query:
var UserList = db.UserInfo
.Join(db.Course, u => u.id, c => c.userid, (u, c) =>
new { u, c }).GroupBy(r => r.u.id)
.Select(g => g.OrderByDescending(r => r.c.datetime)
.FirstOrDefault()).OrderByDescending(a => a.u.datetime).ToList();
Using this query, I don't want user data those who are not in course table, so I want to this data also in course table in userid in or not.
you can use
var qry = Foo.GroupJoin(
Bar,
foo => foo.Foo_Id,
bar => bar.Foo_Id,
(x,y) => new { Foo = x, Bars = y })
.SelectMany(
x => x.Bars.DefaultIfEmpty(),
(x,y) => new { Foo=x.Foo, Bar=y});
ref: How do you perform a left outer join using linq extension methods

asp.net mvc 4 linq extension method syntax join

var model = _db.Seuni
.Join(from o in _db.Keuni
join
c in _db.Seuni
on o.klasaid equals c.klasaid
select new {o.emriklases,c.emristudent,c.studentid,c.nota })
.OrderByDescending(r => r.nota)
I have this sample of code,I have searched to find the right syntax for this linq extension method ,and I haven't can sb give to me the right one?Thank You in advance
You have mixed query and method syntax in a wrong way.
var model = _db.Seuni
.Join(_db.Keuni,
c => c.klasaid,
o => o.klasaid,
(c, o) => new { o.emriklases, c.emristudent, c.studentid, c.nota })
.OrderByDescending(r => r.nota);
Or with query (method for order) syntax
// query syntax
var model = (from o in _db.Keuni
join c in _db.Seuni on o.klasaid equals c.klasaid
select new { o.emriklases, c.emristudent, c.studentid, c.nota })
.OrderByDescending(r => r.nota);

How to join 3 tables with lambda expression?

I have a simple LINQ lambda join query but I want to add a 3rd join with a where clause. How do I go about doing that?
Here's my single join query:
var myList = Companies
.Join(
Sectors,
comp => comp.Sector_code,
sect => sect.Sector_code,
(comp, sect) => new {Company = comp, Sector = sect} )
.Select( c => new {
c.Company.Equity_cusip,
c.Company.Company_name,
c.Company.Primary_exchange,
c.Company.Sector_code,
c.Sector.Description
});
I want to add the following SQL command to the above LINQ query and still maintain the projections:
SELECT
sector_code, industry_code
FROM
distribution_sector_industry
WHERE
service = 'numerical'
The 3rd join would be made with Sector table & Distribution_sector_industry on sector_code.
Thanks in advance.
Just a guess:
var myList = Companies
.Join(
Sectors,
comp => comp.Sector_code,
sect => sect.Sector_code,
(comp, sect) => new { Company = comp, Sector = sect })
.Join(
DistributionSectorIndustry.Where(dsi => dsi.Service == "numerical"),
cs => cs.Sector.Sector_code,
dsi => dsi.Sector_code,
(cs, dsi) => new { cs.Company, cs.Sector, IndustryCode = dsi.Industry_code })
.Select(c => new {
c.Company.Equity_cusip,
c.Company.Company_name,
c.Company.Primary_exchange,
c.Company.Sector_code,
c.Sector.Description,
c.IndustryCode
});
Okay, I can't see why you'd want to select sector_code when you already know it, but I think you want this:
var query = from company in Companies
join sector in Sectors
on company.SectorCode equals sector.SectorCode
join industry in DistributionSectorIndustry
on sector.SectorCode equals industry.SectorCode
where industry.Service == "numerical"
select new {
company.EquityCusip,
company.CompanyName,
company.PrimaryExchange,
company.SectorCode,
sector.Description,
industry.IndustryCode
};
Notes:
I've changed it into a query expression as that's a much more readable way of expressing a query like this.
Although the "where" clause comes after the join, assuming this is a LINQ to SQL or Entity Framework query, it shouldn't make any difference
I've lengthened the range variable names for clarity
I've converted your other names into conventional .NET names; you can do this too in your model
For 4 Tables
var query = CurrencyDeposits
.Join(Customers, cd => cd.CustomerId, cus => cus.Id, (cd, cus)
=> new { CurrencyDeposit = cd, Customer = cus })
.Join(Currencies, x => x.CurrencyDeposit.CurrencyId, cr => cr.Id, (x, cr)
=> new { x.CurrencyDeposit, x.Customer, Currency = cr })
.Join(Banks, x => x.CurrencyDeposit.BankId, bn => bn.Id, (x, bn)
=> new { x.CurrencyDeposit, x.Customer, x.Currency, Bank = bn})
.Select(s => new {
s.CurrencyDeposit.Id,
s.Customer.NameSurname,
s.Currency.Code,
s.Bank.BankName,
s.CurrencyDeposit.RequesCode
});
Try something like this...
var myList = ({from a in Companies
join b in Sectors on a.Sector_code equals b.Sector_code
join c in Distribution on b.distribution_code equals a.distribution_code
select new {...});

Categories

Resources