I am trying to assign linq datasource in code behind but I have IQueryable query want to assign in where clouse using Any function like a sub query clause in sql
this is my sql statment
select * from table1 where col1 in (select col1 from table1 where col2 like '%xx%')
how to convert this clouse to bind it into linq datasource code behind
You can convert this query in linq.
var result = from c in db.table1
where db.table1.Any(e => e.col2.Contain("xx"))
select c;
It sounds like you need to call .ToList() on the query that returns IQueryable.
Related
I am trying to convert an expression to lambda, but I can't do it correctly because the result executing it in the database and in the lambda query gives different result.
SQL Query:
select tab1.* from table1 tab1 inner join table2 tab2 on tab1.id = tab2.id
That query gives me 16 rows but I can't make a lambda expression that doesn't return an empty list.
My Lambda expression:
var query = table1.Join(table2, tab1=> tab1.id, tab2=> tab2.id, (tab1, tab2) => new {tab1}).ToList();
I am writing a simple LINQ query. Table structure is defined below:
Table A
Id int,
VName varchar2(20),
VAddress varchar2(200)
Table B
Id int,
NName varchar2(20),
NAddress varchar2(200)
LINQ Query
from c in A
join x in B on c.Id equals x.Id
order by A.Id
select New{
c.Id,
x.NName
}
Then SQL Generate as
select Filter1.Id,Filter1.NName from(
Select Extend1.Id,Extend1.VName,Extend1.VAddress,
Extend2.Id as Id1,Extend2.NName,Extend2.NAddress
from A as Extend1 Inner Join B as Extend2 on Extend1.Id=Extend2.ID)
as Filter1
MY Problem: I don't want select many columns in SubQuery.
If you're really worried about performance, then just use plain SQL... it's just about always faster than Linq (in my personal experience). Also, you can try using this Linq to execute your SQL:
IEnumerable<YourDataType> result = DataContext.ExecuteQuery<YourDataType>(SqlString);
You can find out more from the DataContext.ExecuteQuery<TResult> Method (String, Object\[\]) page at MSDN. Please note that this page relates to a different overload of this method, but it has examples in it.
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 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)
};
I'm using LINQPad and I would like to know schema details of a table.
I know that I do it using SQL:
SELECT column_name,*
FROM information_schema.columns
WHERE table_name = '{table_name}'
ORDER BY ordinal_position
How can I do this using LINQ?
LINQ to SQL contexts have a Mapping property that you can use for this sort of thing. A query like the one you provided might look something like this:
from t in context.Mapping.GetTables()
where t.TableName == "[table_name]"
from c in t.RowType.DataMembers
orderby c.Ordinal
select new {columnName = c.Name, columnInfo = c}
See this answer for more details.
MetaTable t = MyDataContext.Mapping.GetTables().Where(
i => i.TableName == "TABLENAME").SingleOrDefault();
PropertyInfo[] fields = t.RowType.InheritanceRoot.GetType().GetProperties();
'fields' will contain the names and types of the columns.
In LINQ to SQL, you could try to bring in these views into the model. If that does not work, you could create a stored procedure that retrieves the info, then LINQ to SQL maps the stored procedure and you execute it as a function.