Query with case statement from SQL Server to Linq query c# [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 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());

Related

ASP.NET using Entity Framework display data to drop down list with distinct [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 2 years ago.
Improve this question
Please I need to implement distinct in a.CompanyName but it doesn't work. What do I need to do ?
try
{
var listCompany = company.CompanyList();
companyList1.DataSource = listCompany.Select(a => new { a.CompanyName, a.CompanyId }).Distinct();
companyList1.DataTextField = "CompanyName";
companyList1.DataValueField = "CompanyId";
companyList1.DataBind();
}
catch (Exception)
{
throw;
}
You can use with .GroupBy()
Groups the elements of a sequence.
companyList1.DataSource = listCompany
.GroupBy(x => x.CompanyName)
.Select(a => new
{ CompanyName = a.First().CompanyName, CompanyId = a.First().CompanyId });
Above query will group your result by company name and then will select CompanyName and CompanyId of first record
Try out online

Convert SQL query into linq 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 3 years ago.
Improve this question
Convert SQL query to LINQ C#
SELECT ts.TeacherId, count(Distinct ts.SubjectId) as Subjects
from dbo.TeacherSubjects ts
GROUP BY ts.TeacherId
HAVING ts.TeacherId = 2352
here is my LINQ query
var SubjectsGroup = db.TeacherSubjects.Where(p => p.TeacherId == 2352).Distinct().GroupBy(x => x.TeacherId);
var teacherId = ; // your teacherId here
var result = new {
TeacherId = teacherId,
Subjects = db.TeacherSubjects.Where(ts => ts.TeacherId == teacherId).Select(x => x.SubjectId).Distinct().Count()
};
Or you can group by SubjectId:
var teacherId = ; // put your teacherId here
var result =
(from ts in TeacherSubjects.Where(x => x.TeacherId == teacherId)
group ts by ts.SubjectId into gr
select new
{
TeacherId = teacherId,
Subjects = gr.Count()
}).ToList();

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

how to sort date 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 7 years ago.
Improve this question
I have many date in one variable like:
var date = hfAllDates.Value.Split('#');
there are four date(01/01/2016,22/01/2016,18/01/2016,05/01/2016) in date variable .
how to sort date.
Thanks
add namespace using System.Linq;
and use LINQ Query
in ascending order
var orderedDateList = date.OrderBy(x => DateTime.Parse(x)).ToList();
in Descending oreder
var orderedDateList = date.OrderByDescending(x => DateTime.Parse(x)).ToList();
Convert date from string[] to List<DateTime> first:
List<DateTime> dtList = date.Select(x => DateTime.Parse(x)).ToList();
Then you can easily sort it:
dtList.Sort();
Using Linq's OrderBy:
var sortedDates = date.OrderBy(x => DateTime.Parse(x));
or
var sortedDates = date.OrderByDescending(x => DateTime.Parse(x));
if you are needed to sort dates descending
I suggest using Linq:
var date = hfAllDates.Value.Split('#')
.Select(line => DateTime.Parse(x))
.OrderBy(x => x)
.ToList();

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

Categories

Resources