LINQ Query can return the counts of each column of DataTable - linq

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

Related

Oracle - Update COUNT of rows with specific value

This question has been posted several times but I am not able to get it work. I tried the approach mentioned in Update column to the COUNT of rows for specific values in another column. SQL Server. It gives me SQLException: java.sql.SQLException: ORA-01427: single-row subquery returns more than one row error not sure what I can do.
Below is my problem
UPDATE dataTable
SET ACCX = (select b.cnt
from dataTable a
join
(SELECT Account,
COUNT(1) cnt
FROM dataTable
GROUP BY Account) b
on a.Account=b.Account)
,ACCR = 15481
,ACCF = 3
WHERE ID = 1625
I only have the access & can change to the bold part since rest of the query is generated by the tool I cannot change it & I have to update ACCX column with the count of the value in column Account. Is it possible to do?
Note: - Account column is already populated with values.
You cannot follow that query because it is for sqlserver and NOT oracle. It is simpler in oracle and does not need a join to itself.
This update will set the count for id 1625 only based on number of account numbers in datatable. See demo here;
http://sqlfiddle.com/#!4/d154c/1
update dataTable a
set ACCR = 15481,
ACCF = 3,
a.ACCX = (
select COUNT(*)
from dataTable b
where b.Account=a.Account)
WHERE a.ID = 1625;

getting values from a table where value is not null

When doing an EF query in the code behind using LINQ, how does one go about retrieving only those items in a nullable column that actually have data?
for example;
Dim unit = (From d in ctx.Inventories
Where d.ProductId Is Not Null
Select d).ToList()
Where obviously that query doesnt work, how does one go about this?
Since ProductId is likely a nullable type, you should be able to do:
Dim unit = (From d in ctx.Inventories
Where d.ProductId.HasValue
Select d).ToList()

How do I insert 10000 values into Oracle and keep them in the right order?

I'm trying to insert 10000 values into Oracle from C# and am using the code below. It is VERY important that the order of the values in the array that I pass in are maintained in Oracle, however every time I run this the order of the values changes. What can I change to stop the order changing, or is there a completely different approach that I can use to insert 10000 values in a set order that can be retrieved easily?
The datalist is an array of type double and size 10000.
var oc = new OracleConnection(ConnectionString);
oc.Open();
var transaction = oc.BeginTransaction();
OracleCommand command = new OracleCommand("",oc);
command.CommandText = ("INSERT INTO DEMO (DISTRIBUTIONSLICES) values (:DISTRIBUTIONSLICES)");
OracleParameter distributionslices = new OracleParameter("DISTRIBUTIONSLICES",OracleDbType.Varchar2, ParameterDirection.Input);
distributionslices.Value = datalist;
command.Parameters.Add(distributionslices);
command.ArrayBindCount = 10000;
command.ExecuteNonQuery();
Any help would be greatly appreciated.
Paradigm error: Rows in a SQL database table don't, in general, have an order you can rely on.
If you want to be able to retrieve rows in a known order, you need to do something explicit to make that happen, such as adding an extra column of unique integers which defines the order you want.
You can then retrieve your rows with ORDER BY on your ordering column, and it will give you the rows in the order you want.
Create a sequence in database(lets name it seq).
Alter table add (seq_id number);
INSERT INTO DEMO (DISTRIBUTIONSLICES, seq_id) values
(:DISTRIBUTIONSLICES, seq.nextval())
select in this way:
Select *
from demo
order by seq_id

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;

Adding a random Guid column to a Linq to Entities query to grab random records

I've found some articles about using the RandomView view and the GetNewID function to pull back randomized records, but they are using this method with Linq to SQL which allows Functions and Stored Procs to be used with no return value or a scalar return value. From what I understand, a Stored Proc has to been returned as one of the Entity Framework objects from my generated model. I have been able to get that to work as an object, but not returning a scalar or no return set.
What I want to do is to simply add a column to my Linq query that contains a newly generated Guid so that I can order by the new Guids and take a specific number of records randomly. Can anyone help with some sort of lambda expression or join that would enable me to do this? It seems like it should be something built into EF, but I understand that we are on EF v1.
(Please provide code in VB.net)
In the Select clause of your Linq query, you should be able to insert a GUID like this:
var result = from myRecord in myTable
select new {
field1 = myRecord.field1,
field2 = myRecord.field2,
guidField = Guid.NewGuid()
};
Well, my VB is a little rusty, but I think this will work...
Dim result =
From myRecord in myTable _
Select field1, _
field2, _
guidField = System.Guid.NewGuid()

Resources