linq and datagridview - linq

why does the following show only the length of the fields inside the grid and not the data?
(column header in grid is also length)
dim q as IQueryable = (from p in DB.Table select p.field1)
while this shows both columns and tha data
dim q as IQueryable = (from p in DB.Table select p.field1, p.field2)
both fields are strings
thanks in advance

Related

LINQ Query can return the counts of each column of DataTable

I have a query, I am using a data table and I want to see each variable how many rows have values without null and value(-1) in that particular column. If anyone can help me to use the LINQ query and check the numbers.
Example: I have a table (City, Age, Gender) and i want to show the user that which column has blanks.... I mean in my current dataset city & Age has 200 rows but gender has 170 due to punching error so I through the loop to check each column I want to show the counts.
Here is what I have tried but that is not working. And its Vb.net code.
Dim dtRaw As New DataTable
daGlobal = New OleDb.OleDbDataAdapter(CMD, MyConnection)
daGlobal.Fill(dtRaw)
daGlobal.Fill(dtGlobal)
For Each column As DataColumn In dtRaw.Columns
Dim xss As String = column.ColumnName
Dim MyCounts = (From xss In dtRaw.AsEnumerable()
Where (Not String.IsNullOrEmpty(xss)).Count())
Next

ASP.net LINQ on DataView to use Like query

I want to filter a DataView but DV.Rowfilter is taking too much time .
dv.RowFilter = "ProductName like '%" + searchtxt + "%'";
So I decided to use the LINQ but how to implement above like query in LINQ ?
LINQ is not more efficient in general, but it can increase readability and maintainability.
So you can try Linq-To-DataSet:
var query = from row in dv.Table.AsEnumerable()
let productName = row.Field<string>("ProductName")
where productName.Contains(searchtxt)
select row;
DataTable tbl = query.CopyToDataTable(); // use this as DataSource or use tbl.DefaultView
Here the same with method syntax:
var query = dv.Table.AsEnumerable()
.Where(row => row.Field<string>("ProductName").Contains(searchtxt));
MSDN: Creating a DataView Object with LINQ to DataSet
i have tried your second solution, but now its throwing the exception
"The source contains no DataRows." and actually the DataTable which i
make as DataTable.AsEnumerable() , it has the rows in it
The table contains rows but the filter skips all of them.
You could use if(query.Any()){...} to check if there are rows:
DataTable tbl = dv.Table.Clone(); // creates an empty table with the same columns
if(query.Any())
tbl = query.CopyToDataTable();

Linq: Orderby when including multiple tables

Currently learning Linq to Entity. I been successful, but came stumped with the orderby clause and its use with multiple tables.
var query = from k in contxt.pages.Include("keywords")
where k.ID == vals.pageId select k;
My understanding with the code above is it creates an inner join where ID is equal to pageId.
So what I am having a difficult time visualizing is how I would perform an orderby on both tables?
I would like to sort on both tables.
I have tried:
var query = from k in contxt.pages.Include("keywords") where k.ID == vals.pageId orderby k.keywords.**?** select k;
The question mark is not supposed to be there. I am showing that the column that I would like to sort by isn't there. Trying this k.Kegwords. doesn't show the column.
I would write a SQL query as follows:
string query = "SELECT pages.page, pages.title, pages.descp, keywords.keyword
FROM pages INNER JOIN keywords ON pages.ID = keywords.pageID
ORDER BY keywords.sort, pages.page";
pages and keywords have a 1 to many relationship, which FK keywords.
Thank you,
deDogs
Here you go.
var result = (from x in pages
join y in keywords on x.ID equals y.pageID
orderby y.sort, x.page
select new
{
x.Page,
x.title,
x.descp,
y.keyword
});

LINQ self join in ASP.NET MVC3

I have a situation where I need to do a self join on a table in LINQ. The table consists of fields ItemsID, Title, SeriesTitle, and many other. An item can be either a series or members and I can tell that by looking into ItemId which has "S" or "M" letters on it. I need to retrieve all records that are member of a series with ItemId "S117". I can do this in simple SQL by the code below,
select i.Series_Title, i.Item_ID, i2.Item_ID as Member_ID,
i2.Title as Member_Title, i2.Series_Title as Member_Series_Title
from Items i join Items i2 on i.Series_Title = i2.Series_Title
where i.Item_ID = "S117"
Now, I translated this query in LINQ which goes as
items = _dataContext.Items.AsQueryable();
items = from series in items
join members in items on series.Series_Title.ToLower()
equals members.Series_Title.ToLower()
where series.Item_ID.ToLower().Equals(itemId)
select series;
The last line of this query select series will only retrieve series but not members and I need members also.
I am using MVC3 Razor view where I have to display almost all fields so I am not using select new {....}
Even when I tried to use select new {series, members}, I got this exception -
Cannot implicitly convert type
'System.Linq.IQueryable'
to 'System.LinQ.IQueryable<My.App.models.Items>'
An explicit conversion exist.
Any suggestions would be highly appreciated.
Try this:
var items1 = _dataContext.Items.AsQueryable();
var items2 = from series in items1
join members in items1 on series.Series_Title
equals members.Series_Title
where series.Item_ID== 'S117'
select series;

Linq query ("not in ") on datatable

I would like to return all rows from TableA table that does not exists in another table.
e.g.
select bench_id from TableA where bench_id not in (select bench_id from TableB )
can you please help me write equivalent LINQ query. Here TableA is from Excel and TableB is from a Database
I am loading Excel sheet data into DataTable, TableA. TableB I am loading from Database. In short, TableA and TableB is type of DataTable
So if table A is from Excel, are you loading the data into memory first? If so (i.e. you're using LINQ to Objects) then I suggest you load the IDs in table B into a set and then use:
var query = tableA.Where(entry => !tableBIdSet.Contains(entry.Id));
If this isn't appropriate, please give more details.
Converting into a set is probably best done just by using the HashSet constructor which takes an IEnumerable<T>. For example:
var tableBIdSet = new HashSet<string>(db.TableB.Select(entry => entry.Id));
(If the IDs aren't actually distinct, you could add a call to Distinct() at the end.)
var lPenaltyEmployee = from row1 in tBal.getPenaltyEmployeeList().AsEnumerable()
select row1;
var PenaltyEmp = new HashSet<string>(lPenaltyEmployee.Select(Entry => Entry.Emsrno);
DataTable lAbsentEmp = (from row in tBal.getAbsentEmployee(txtFromDate.Text).AsEnumerable()
where !(PenaltyEmp).Contains(row["Emsrno"].ToString())
select row).CopyToDataTable();
From a in TableA
Group Join b in TableB on a.bench_id Equalsb.bench_id into g = Group
Where g.Count = 0
Select a

Resources