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()
Related
I am quite new to this, I am running two SQL queries and I am creating two separate data tables, DataTable1 and DataTable2.
I am applying some linq criteria to DataTable1 and creating another data table from that, which is DataTable3.
var Query3 = from table1 in DataTable1.AsEnumerable()
where table1.Field<DateTime>("DateTime") <= Yday
where table1.Field<string>("StockCode").Contains("-CA") && !(table1.Field<string>("StockCode").Contains("-CAB")) ||
table1.Field<string>("StockCode").Contains("-CM") ||
table1.Field<string>("StockCode").Contains("-LP")
select table1;
DataTable DataTable3 = Query3.CopyToDataTable()
Now I would write another query to do the following.
Both data tables have a column JobNumber. I would like to query DataTable3 in DataTable 2 to count the rows that have similar JobNumber entries. Below is what I am doing but I am not getting the correct count.
int count = (from table3 in DataTable3.AsEnumerable()
join table2 in DataTable2.AsEnumerable() on table2.Field<string>("JobNumber") equals table3.Field<string>("JobNumber")
where table2.Field<string>("JobNumber") == table3.Field<string>("JobNumber")
select table2).Count();
You are creating a cartesian join and counting its result, was that what you indented ? Also in your linq your Join expression and where expression is same (where is redundant). It is not clear what you really want to count. Probably you instead wanted to count those in DataTable2 where JobNumbers exists in DataTable3?:
var jobNumbers = (from r in DataTable3.AsEnumerable()
select r.Field<string>("JobNumber")).ToList();
var count = (from r in DataTable2.AsEnumerable()
where jobNumbers.Contains( r.Field<string>("JobNumber") )
select r).Count();
As a side note, it would be much easier if you used Linq To SQL instead (rather than Linq To DataSet).
Let's say I have a list with 2 or more customerIds and a list with two or more order dates. I want an SQL query like this from linq to sql
SELECT *
FROM Orders
WHERE (CustomerId = #CustomerId1
AND (OrderDate = #OrderDate1 OR OrderDate = #OrderDate2))
OR
(CustomerId = #CustomerId2
AND (OrderDate = #OrderDate1 OR OrderDate = #OrderDate2))
The list with CustomerIds and order dates is not fixed, so I need to loop through it when building the query.
I found a solution for this by using PredicateBuilder
from http://www.albahari.com/nutshell/predicatebuilder.aspx
I'm using a dbcontext linq query:
var list = context.MyTable.Where(x => x.IsValid).ToList();
The SqlProfiler shows this Sql Query:
SELECT * FROM [MyTable] WHERE IsValid = 1
The problem is that in this table I'm using a lot of sql indexes, and by default it uses the wrong index and query is taking a very long time. I need to add the index I have in the table into the query.
In other words how to get this query from linq?
SELECT * FROM [MyTable] WITH(INDEX(PK_MyIndexName)) WHERE IsValid = 1
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.
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.