I'm trying to achieve this in c#
Select a.Name,a.Param
from Customization a
where a.name in (select Name from Standard)
I have try something like this but it still doesn't work.
merge = dt1.AsEnumerable()
.Where(r => r.Field<string>("Name")
.Contains(dt2.Rows.Contains("Name")))
.CopyToDataTable();
By using the current way we need to get the name list from the second data-table(dt2) for each row in dt1 so I suggest you get the list of names first and then check whether r.Field<string>("Name") contains in the collection. For this, you can use the following code
var NameCollection = dt2.AsEnumerable().Select(x=> x.Field<string>("Name")).ToList();
merge = dt1.AsEnumerable()
.Where(r => NameCollection.Contains(r.Field<string>("Name")))
.CopyToDataTable();
Related
I have tow tables - OrderRequisition and Order. I can show all the records from OrderRequisition table using linq query:
var list = (from r in db.OrderRequisition
select new SalesOrderViewModel
{
OrderId = r.OrderId ,
OrderNo = r.OrderNo
}).ToList();
I want to show only those records from OrderRequisition table which are not included in Order table. Any clue
Thanks
Partha
A simple approach that might be efficient enough because your database is able to optimize it:
var list = db.OrderRequisition
.Where(or => !db.Order.Any(o => o.OrderId == or.OrderId))
.ToList();
(skipped the SalesOrderViewModel initialization because not relevant for the question)
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();
Assume there is a string of text values comma-separated, like an array:
var excludelist ="apples,oranges,grapes,pears";
The excludelist values may come from a query to a table in a database.
Assume a query wherein we want to return all rows EXCEPT those rows where the field named Fruit contains any items from the excludelist.
var qry = from s in context.Groceries.Where(s => s.Fruit(here is where we need to exclude the items??) join u in context.Users on s.Owner equals u.User_ID
Can someone provide a sample Link to SQL answer?
Did you try Except?
var qry = from s in context.Groceries.Except(excludelist)...
Link to SQL has CONTAINS (and!CONTAINS)
where !excludelist.Contains(i)
select i;
I solved it this way:
I have a database table with the names of the fruits I wish to exclude and I created a List (IList apparently won't work).
List<string> excludedfruit = context.ExcludedFruit.Select(x => x.ExcludedFruitName).ToList();
Then I used the following Linq to SQL query (partially shown)
var qry = from s in context.Groceries
.Where(s => !excludedfruit.Contains(s.Fruit))
When trying to use GROUPBY I get an error saying a field 'date1' is not found on selected resource.
var query = (from a in db.Dates
from b in db.Facts
where a.Count_Key == b.Date_key
select new{
a.Date1,
a.Month,
b.Fact_key
});
var query2 = query.GroupBy(x => x.Month );
Grid1.DataSource = query2;
Grid1.DataBind();
So, when I try to bind with query it works perfectly, but query2 yields the error
field date1 not found on selected datasource.
How can I fix this?
The group by returns columns with different names than the original select.
You can do the following to see it "for educational purpose".
create a new Grid2 and use it with automatic columnmapping. Bind Query2 to this Grid2 and you will see the result.
Because you grouped by month, now you have only month field as a key, and collection of grouped items. If you want more fields on data source you need to use aggregate function on date1 field.
For example like this:
var query2 = (from q in query
group q by q.Month into g
select new
{
Month = g.Key,
Date = g.Select(gg=>gg.Date1).Max //or you can use first here etc.
}).ToList()
hope this helps
I want to use Linq to ADO.NET to fetch all rows that match the criteria below from a DataTable.
Select all rows where "parentId" equals "id" of a row where "parentId" equals null.
Order by "Name".
Can someone tell me how to accomplish this (preferably using both Query Syntax and Method Syntax), and possibly point me to where I can read more about this topic?
There is no such thing as "Linq to ADO.NET" (perhaps you're confusing with ADO.NET Entity Framework). In your case, you seem to be referring to Linq to DataSets
You could do something like that :
Query syntax :
var parents = from row in table.AsEnumerable()
where row.IsNull("parentId")
select parents;
var children = from row in table.AsEnumerable()
where parents.Any(p => p.Field<int>("id") = row.Field<int>("parentId"))
orderby row.Field<string>("Name")
select row;
Method syntax :
var parents = table.AsEnumerable()
.Where(row => row.IsNull("parentId"));
var children = table.AsEnumerable()
.Where(row => parents.Any(p => p.Field<int>("id") = row.Field<int>("parentId")))
.OrderBy(row => row.Field<string>("Name"));