How to change the following SQL query to Linq query and how to convert results to a list of strings?
select Name
from Categories
where ID in (select CID from CategoryLink where VID = 57)
Please, provide some extra context. From the fist sight the code should be something like this:
List<string> names = db.Categories
.Where(c => db.CategoryLink.Any(cl=>cl.VID == 57 && CID == c.ID))
.ToList();
Related
I have a sql statement as below, I want to translate it into Linq
select *
from
(
select Top 12 *
from DailyData
where ddaCode = '600000' and ddaDate < '2008/12/31'
order by ddaDate desc) as X
order by ddaDate
How can I do it? Thank you.
The From subquery becomes your first Linq query. Then this is queried in the second one.
var fromResults = DailyData.Where(x => x.ddaCode == "600000"
&& x.ddaDate < new DateTime(2008,12,31)
.OrderByDescending(x => x.ddaDate)
.Take(12);
var results = fromResults.OrderBy(x => x.ddaDate)
I have a table named dbo.Question having two columns Id and Title. I want to write LINQ query to get all questions where the question Title begins with numbers.
The following is a sample of question search result where the "Title" starts with a number:
5 Useful Visual Studio shortcuts
7 Things to know about Hadoop.
10 Reasons You Should Be Thankful For Hackers
The SQL query is like:
SELECT Title FROM dbo.Question WHERE Title NOT LIKE '[a-z]%'
What will be the LINQ equivalent of the above SQL query?
var query = from x in dbcontext.Questions
where SqlFunctions.IsNumeric(EntityFunctions.Left(x.Title, 1)) == 1
select x;
Source:
http://msdn.microsoft.com/en-us/library/system.data.objects.sqlclient.sqlfunctions.isnumeric(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/dd383069(v=vs.110).aspx
Come to think of it, this isn't very efficient, given that it doesn't use indices. You can instead do this...
IEnumerable<string> digits = Enumerable.Range(0, 10) // 0-9
.Select(i => i.ToString());
//Create a query for starts with on each digit.
IEnumerable<IQueryable<Question>> questions = digits
.Select(i => dbcontext.Questions.Where(q => q.StartsWith(i))
IQueryable<Question> concatedTogether = .Aggregate(Queryable.Concat) //Union all each of them together
int count = concatedTogether.Count();
OR simply
int count = Enumerable.Range(0, 9)
.Select(int.ToString)
.Select(i => dbcontext.Questions.Where(q => q.StartsWith(i))
.Aggregate(Queryable.Concat)
.Count();
Can the below query be achieved with LINQ to SQL?
select id,
(select StateName from b_mstates where id=StateIdFk) as [State Name],
CityName
from b_mcities
var result = b_mcities.where(x => b_mstates.where(y => y.id == StateIdFK)
.contains(x.statename)).ToList();
Your question is pretty tough, because your sql wont run(it is malformed and missing logic). this is the jist of what you will need to do.
Assuming you have your associations set up correctly, it should be as simple as
var result = from city in m_bcities
select new {city.id, city.State.StateName, city.CityName}
I have used too much time (days) on this and I really hope someone can help me out.
I found a good article on describing my problem in a generic way so let's stick to it.
I am trying to build this query but NHibernate fails to build the correct sql and returns a sql query exception.
Column vSagsAendring.Id is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. It could not execute the following query:
select
viewsagsae0_.Id as Id155_,
viewsagsae0_.SagId as SagId155_,
viewsagsae0_.JournalNr as JournalNr155_,
viewsagsae0_.LbfNr as LbfNr155_,
viewsagsae0_.OrgNr as OrgNr155_,
viewsagsae0_.OrgNavn as OrgNavn155_,
viewsagsae0_.AfdNavn as AfdNavn155_,
viewsagsae0_.SagsType as SagsType155_,
viewsagsae0_.Status as Status155_,
viewsagsae0_.SagsbehandlerInit as Sagsbeh10_155_,
viewsagsae0_.Dato as Dato155_,
viewsagsae0_.JournalAktionType as Journal12_155_,
viewsagsae0_.Beskrivelse as Beskriv13_155_,
viewsagsae0_.Ekstern as Ekstern155_
from vSagsAendring viewsagsae0_
group by viewsagsae0_.SagId
var query = from p in _session.Query<ViewSagsAendring>()
group p by p.SagId
into grp
select grp.OrderByDescending(g => g.Dato).First();
This is another version also took from the article:
var query = from p in _session.Query<ViewSagsAendring>()
group p by p.SagId
into grp
let maxDato = grp.Max(g => g.Dato)
from p in grp
where p.Dato == maxDato
select p;
It's have been a long journey, but now it's over. I hope that I can help someone else in the same situation by answering my own question.
var aendring = from sagsAendring in _session.Query<ViewSagsAendring>()
where sagsAendring.Dato ==
(
from innersagsAendring in _session.Query<ViewSagsAendring>()
where innersagsAendring.SagId == sagsAendring.SagId
select innersagsAendring.Dato
).Max()
select sagsAendring;
var result = aendring.ToList();
And because you can chain linq statements you can build a linq filter like this
if(Filters.VisInterneAendringer == false)
query = query.Where(x => x.Ekstern == true);
if (Filters.VisKunNyesteAendringer)
{
query = query.Where(sagsAendring => sagsAendring.Dato ==
(
from innerSagsAendring in Session.Query<ViewSagsAendring>() where innerSagsAendring.SagId == sagsAendring.SagId
select innerSagsAendring.Dato
).Max());
}
return query;
Your queries seem legit for LINQ in EntityFramework.
I'm not sure about hibernate, you might try to use QueryOver API instead of Query
http://nhibernate.info/blog/2009/12/17/queryover-in-nh-3-0.html
I need help to convert following ql query to Linq to Sql query.
select Name, Address
from Entity
group by Name, Address
having count(distinct LinkedTo) = 1
Idea is to find all unique Name, Address pairs who only have 1 distinct LinkedTo value. Remember that there are other columns in the table as well.
I would try something like this:
Entity.GroupBy(e => new { e.Name, e.Address})
.Where(g => g.Select(e => e.LinkedTo).Distinct().Count() == 1)
.Select(g => g.Key);
You should put a breakpoint after that line and check the SQL that is generated to find what is really going to the database.
You could use:
from ent in Entities
group ent by new { ent.Name, ent.Address } into grouped
where grouped.Select(g => g.LinkedTo).Distinct().Count() == 1
select new { grouped.Key.Name, grouped.Key.Address }
The generated SQL does not use a having clause. I'm not sure LINQ can generate that.