linq convert query syntax to method syntax [closed] - c#

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

Related

how to write a nested Sql query in Linq [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
so i have this query in sql:
select (select ConfigItemDescripcion from SGRC_ConfigItem where ConfigId = 'SEGM' and ConfigItemId = SegmentoId) Segmento,
(select ConfigItemDescripcion from SGRC_ConfigItem where ConfigId = 'MRCA' and ConfigItemId = MarcaId) Marca,
Producto,
Familia
from sgrc_emisor
where EmisorCuenta = '3702406435'
I want to write the same query in a linq expression or a lambda expression.
Thanks for the help in advance
Finally i manage to come up with the query in linq, dont know how to do it in lambda, but it works fine.
var obj = (from emisor in _context.DbSetEmisores
where emisor.EmisorCuenta == cuenta
select new EmisorDto
{
Segmento =
((from itemConf in _context.ItemsDeConfiguracion
where itemConf.ConfigID == "SEGM" && itemConf.ConfigItemID == emisor.SegmentoId
select new { itemConf.ConfigItemDescripcion }).FirstOrDefault().ConfigItemDescripcion),
Marca =
((from itemConf in _context.ItemsDeConfiguracion
where itemConf.ConfigID == "MRCA" && itemConf.ConfigItemID == emisor.MarcaId
select new { itemConf.ConfigItemDescripcion }).FirstOrDefault().ConfigItemDescripcion),
Producto = emisor.Producto,
Familia = emisor.Familia,
SegmentoId = emisor.SegmentoId,
MarcaId = emisor.MarcaId,
}).FirstOrDefault();
When using LINQ you can use either Query syntax as shown in the LINQ below (If you are familiar with SQL then this looks more natural).
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/query-syntax-and-method-syntax-in-linq
The other option is to use Method syntax, and below is a short example. This allows for chaining of methods, biggest thing to keep in mind is "var" should be used, the return type is dynamic and the compiler will help you out a lot if you just use "var"
var items = _list.Where(x => x.Attribute1 == "NextField")
.Where(x => x.Attribute2 == "Something else");
Other things that hangs folks up sometimes is LINQ uses "delayed execution"

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

Query with case statement from SQL Server to Linq query 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 7 years ago.
Improve this question
I have the following query in sql server
select COUNT(mc_owner) as nbr ,
case mc_owner when 'Element1' then 'Element1' else 'others' end Owner
from [dbo].[full]
where (date_reception > '01-01-2015')
group by (CASE mc_owner WHEN 'Element1' THEN 'Element1' ELSE 'others' END)
order by nbr desc
I need to convert it to entityframework linq query
Try this,
var result = fulltableData.Where(x=>x.date_reception > '01-01-2015')
.GroupBy(x=> x.mc_owner.Equals("Element1")?"Element1":"others")
.Select(x=> new{ nbr = x.Count(), Owner = x.Key})
.OrderByDescending(x=>x.nbr);
I understand you want to select the number of occurrences of a certain mc_owner compared to all others. The following LINQ query should produce the same result:
full.Where(f => f.date_reception > new DateTime(2015, 1, 1))
.GroupBy(f => f.mc_owner == "Element1" ? f.mc_owner : "others")
.OrderByDescending(group => group.Count())
.Select(group => new { Owner = group.key, nbr = group.Count());

How to re-write this linq from query syntax to method syntax? [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 8 years ago.
Improve this question
As in title, how to rewrite this linq query into method syntax?
(from row in recordsEpP
where (!string.IsNullOrEmpty(row.Column20)
&& !string.IsNullOrEmpty(row.Column14)
&& string.IsNullOrEmpty(row.Column8))
select new ReportDto{
Status = "P",
ContractNumber = row.ContractNumber,
Count = 1
}).ToList();
You could try something like this:
recordsEpP.Where(row=> !string.IsNullOrEmpty(row.Column20) &&
!string.IsNullOrEmpty(row.Column14) &&
string.IsNullOrEmpty(row.Column8)
).Select(row => new ReportDto{
Status = "P",
ContractNumber = row.ContractNumber,
Count = 1
}).ToList();

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