I have such SQL which select, group and order Url field from Statistic table. getDomain is stored function. I am trying to rewrite this SQL to Linq without any luck. Please someone explain how to do that?
SELECT dbo.getDomain(Url) as url
FROM Statistic
GROUP BY dbo.getDomain(Url)
HAVING COUNT(Url) > 1
ORDER BY COUNT(Url)
First you have to define your UDF in the .DBML file that contains other tables and procedures definitions. Then you can Call any UDF function inline within your LINQ query Like this:
var results = from s in dbo.Statistic
groub s by dbo.getDomain(s.url) into g
where g.Count() > 1
orderby g.Count() ascending
select new
{
URL = dbo.getDomain(g.Key)
};
Related
I have a table in which I want to calculate the rank of my data based on Amount.
var result = await context.Table
.Select(i => new Table {
Rank = // want to calculate the rank based on amount
}).OrderByDescending(i => i.Amount)
.ToListAsync();
I tried using the element index but for that, I need to fetch all the data to the client first.
There's no direct implementation for RANK() in EF. You are better off with using a custom LINQ query to do what you want, like,
var query = from t in context.Table
orderby t.Amount descending
select new
{
Rank = (from o in context.Table
where o.Amount > s.Amount
select o).Count() + 1
};
If you know the SQL query you want to execute, you can create a custom function or stored procedure and then execute it using Entity Framework. Please refer this.
I am attempting to write a Linq to SQL query that returns all rows in a DataTable where a columns value (TxnNumber) is not unique. So far, I have the Linq to SQL below where dt is the DataTable that contains the field TxnNumber. I think that I am pretty close but, intellisense is complaining about the CONTAINS clause. I have tried specifying that I only want to return the TxnNumber field in the sub-select but, it will not compile. Can anyone see what I am doing wrong?
dt.AsEnumerable().Where(u => u.TxnNumber.Contains (dt.AsEnumerable().GroupBy(t => t.TxnNumber).Count() > 1));
Try this
(from r in dt.AsEnumerable()
group r by r.TxnNumber into grp
where grp.Count() > 1
select grp).SelectMany(x=>x).ToList();
I have myself this SQL query
SELECT
db_accounts_last_contacts.id,
dbe_accounts_last_contacts.last_contact_date,
db_accounts_last_contacts.description,
db_accounts_last_contacts.follow_up_date,
db_accounts_last_contacts.spoke_to_person_id,
db_accounts_last_contacts.account_idFROM
db_accounts_last_contacts ,
db_companies
WHERE db_companies.id = db_accounts_last_contacts.account_id
ORDER BY db_accounts_last_contacts.last_contact_date DESC
Which returns my results ordered by last_contact_date.
Now I have my Entity framework query
var query = (from c in context.accounts_companies
select new AccountSearchResultModel()
{
LastContacted = (from calc in context.communique_accounts_last_contacts
where calc.account_id == companyId
orderby calc.last_contact_date descending
select calc.last_contact_date).FirstOrDefault()
});
However when I go ahead and do my ToList on it, my results are never ordered
Here is my table un-ordered
Here is my list ordered using the SQL query
Why isn't my entity framework query not picking up my orderby? Or if it is why am I always pulling out the first one?
You need to choose a Property to sort by and pass it as a lambda expression to OrderByDescending
like this:
.OrderByDescending(x => x.calc.last_contact_date);
I hope this helps.
Linq Orderby Descending Query
Sorry for the late answer,
What I had to do in the end was create a view and import it via the EDMX file and then use that to pull out my results.
I need to get the distinct row values from a table using LINQ. The query i used is
var results = (from statename in dsobject.dbo_statetable select statename).Distinct();
It is giving all the rows of the table named "dbo_statetable" but i need only the distinct statename. What should be the query to achieve this?
Its sql equivalent is select distinct statename from dbo_statetable
You need to specify the property:
var results = (from x in dsobject.dbo_statetable select x.Statename).Distinct();
// ^^^^^^^^^^
The variable after from does not specify the column. It is like a table alias in SQL. Your LINQ statement is roughly equivalent to this SQL:
SELECT DISTINCT * FROM dbo_statetable AS statename
dsobject.dbo_statetable.Select(s => s.statename).Distinct()
I have a LINQ query to a DataTable:
var list = from row in table.AsEnumerable()
group row by row.Field<byte>("ID") into g
select new
{
ID = g.Key,
Name = (from c in g
select c.Field<string>("name")).First(),
Localized = (from c in g
select myDic[c.Field<string>("name"))].First();
};
where ID is a primary column, Name - data from query and Localized - a value from a dictionary where key - that data from query (Name).
Will LINQ cache that data from query for the second select or I have to do this in another way?
And another question: if I'll put dictionary creation in select, will it been crated every time?
LINQ does not do any kind of query analysis/optimizations (see note below). Instead use the C# "let" clause (which maps to the SelectMany method) to capture that value once for reuse in the select statement. Here is your revised query:
var list = from row in table.AsEnumerable()
group row by row.Field<byte>("ID") into g
let name = (from c in g
select c.Field<string>("name")).First()
select new
{
ID = g.Key,
Name = name,
Localized = myDic[name]
};
NOTE: IQueryable providers can technically translate LINQ expressions into more optimal ones if they wanted to, but that's a subject of another discussion and you're using pure "LINQ to Objects" here anyway.
LINQ does not cache data. Therefore, it will perform the dictionary lookup for every element of every group.