Boundfield not returning count from LINQ query - asp.net

I have a grouped LINQ query with a count summary line that I am binding to a gridview but the count field in the gridview just shows 'System.Data.DataRow[]'
What am I doing wrong here? Thanks!
aspx:
<asp:BoundField DataField="Service_Name" HeaderText="Impacted services"/>
<asp:BoundField DataField="Fieldname" HeaderText="Count"/>
vb code:
Dim Services = From Lines In dtMain _
Where Lines.Field(Of String)("ULTIMATE_PARENT_NAME") = lstUPs.SelectedItem.ToString _
Group By Parent_Name = Lines.Field(Of String)("ULTIMATE_PARENT_NAME"), _
Service_Name = Lines.Field(Of String)("THIRD_PARTY_SERVICE_CLEAN") _
Into Fieldname = Group, Count()
grdOuterGridView.DataSource = Services
grdOuterGridView.DataBind()

I'm not an expert on that syntax (I use the extension methods) but it looks to me like Fieldname is being assigned the value Group, or the collection of rows that are being grouped on Parent_Name and Service_Name. Perhaps you need to define a field name for the result of Count()?
ETA: Got it. You should say Group.Count(), not Group, Count().

Related

RDLC Report with subquery dataset

I am new to ASP. I need help in creating a report. I have an SQL query
SELECT StudentID, StudentName, SectionID, ExamID, SubjectID, Subject, Marks, TotalMarks
, Percentage, Grade, (SELECT MAX(Marks) AS Exp1
FROM tbl_Result
WHERE (SectionID = #SectionID)
AND (SubjectID = view_StudentResult.SubjectID)) AS SubjectToper
FROM view_StudentResult WHERE (StudentID = #StudentID)
When I display it in report, all other fields are populated as desired but SubjectToper field is shown blank. Can someone help in doing this in an elegant way?
Try populating that field with Exp1.
You are giving the whole sub-query the alias SubjectToper, but the actual selection is given the alias Exp1.
Check out this explanation of sub queries.
Especially the last example where they assign a sub query to a value. Maybe that will work?
Your SQL would look something like:
SELECT StudentID, StudentName, SectionID, ExamID, SubjectID, Subject, Marks, TotalMarks, Percentage, Grade,
SubjectToper = (SELECT MAX(Marks) FROM tbl_Result
WHERE (SectionID = #SectionID) AND (SubjectID = view_StudentResult.SubjectID))
FROM view_StudentResult WHERE (StudentID = #StudentID)
Notice I removed the aliases and assigned the sub query to SubjectToper.
I hope this works, otherwise I'm afraid I'm out of ideas.

Selecting values between 2 dates

I am currently working on a website that offers tutoring services and I have been stuck on this issue quite a while..
I have 2 text boxes where the user chooses a start date and a finish date, when the user clicks view he would be suppose to see the results in a gridview. I am using FormParameters to insert the date into the query.
SelectCommand of the sqldatasource
SelectCommand="SELECT [Session].Session_num AS Num, [Session].Session_Time_Stamp AS Session_Date, Student.Student_First & ' ' & Student.Student_Last AS Student FROM (([Session] INNER JOIN Student ON [Session].Student_Num = Student.Student_Num) INNER JOIN Class ON [Session].Class_ID = Class.Class_ID) WHERE ((([Session].Session_Time_Stamp) Between #firstdate And #seconddate))">
Parameters of the SqlDataSource
<SelectParameters>
<asp:FormParameter Name="firstdate" Type="DateTime"/>
<asp:FormParameter Name="seconddate" Type="DateTime"/>
</SelectParameters>
This is executed when the user clicks the view button, it is where I set the values of the parameters and execute the sql select.
Dim fdate As DateTime = Format(CDate(txtStartDate.Text), "MM/dd/yyyy")
Dim ldate As DateTime = Format(CDate(txtEndDate.Text), "MM/dd/yyyy")
gridTutor.SelectParameters("firstdate").DefaultValue = fdate
gridTutor.SelectParameters("seconddate").DefaultValue = ldate
gridTutor.Select(DataSourceSelectArguments.Empty)
gridTutorSessions.DataBind()
fdate and ldate are not empty, however after this is executed the query result is empty. Could it be that wrong methods to execute the select?
Edit: I realized the problem is probably with the query and DateTime format. When I transformed my textbox value to DateTime it put it like this #2/20/2014#. However, it doesn't return anything even if there are values between the two dates.
If anybody have an access query with DateTime I would like to see it. Thanks
I managed to fix it by formatting the date in my access database it's not the best solution but it is a fix for the situation
I believe you need to manually set fdate and ldate just before you do your 'selectparameters' (i.e. use "fdate = Format(CDate(txtStartDate.Text), "MM/dd/yyyy")". According to the following, values asigned to Dim in the manner you have would not reflect the realtime values you want. See the following regarding VB: "You can assign a value to a variable when it is created. For a value type, you use an initializer to supply an expression to be assigned to the variable. The expression must evaluate to a constant that can be calculated at compile time.

where clause in linq list

H, I have a list of orders and I'm trying to filter this list. So far I have:
Dim orders = _orderController.LoadAll().ToList()
which does indeed give me a list of orders which I can display on a gridview.
How could I filter this list to say: where order.referencenumber = "abc123"
and only give me one order in the list to display in the gridview
Cheers,
Try this:
VB:
Dim orders = _orderController.LoadAll().Where(Function(c) c.referencenumber = "abc").ToList();
C#:
var orders = _orderController.All().Where(o => o.referencenumber = "abc123").ToList();
You should apply the Where clause before your call to ToList
Dim orders = _orderController.LoadAll().Where(Function(c) c.referencenumber = "abc").ToList()
you should check you the examples found in the documentaion

ASP.Net LINQ to SQL group by syntax for System.Generic.IEnumerable Collection

.Net application calls Stored Procedure and returns collection which is stored as 'result.' I want to be able to group the results by 'CreatorLineOfBusinessID'. It returns that field along with several other fields. I would like to use a LINQ query on the 'result' data set. Thanks in advance.
Dim result As IEnumerable(Of eRefer_Reports.uspReport_ReferralsSentBetweenLinesOfBusinessResult)
strFromLOBID = " "
Session("FromLineOfBusiness") = strFromLOBID
strToLOBID = " "
Session("ToLineOfBusiness") = strToLOBID
result = repository.GetQueryResults(CDate(Me.txtStartDate.Text), CDate(Me.txtEndDate.Text), strFromLOBID, strToLOBID)
BindGridView(result)
Have you done any research on Linq grouping? Try the 101 examples here: http://msdn.microsoft.com/en-us/vstudio/bb688088
Dim orderGroups = From p In results Group p By Key = p.CreatorLineOfBusinessID Into Group _
Select CreatorBusinessId = Key, Referrals = Group
Then just iterate over orderGroups using the Key and the property Referrals which will be an IEnumerable<eRefer_Reports.uspReport_ReferralsSentBetweenLinesOfBusinessResult>
For Each lobBusiness in orderGroups
' Get the lobBusinessId : .CreatorBusinessId
' Get the IEnumerable results: .Referrals
Next
Dim query = result.GroupBy(Function(x) x.ID).ToList()
Should group the results the way you want.

perform "not in" query using linq from 2 datatables

i have a datatable which contains "InvalidCodes".
Before uploading the data to database(data is still in datatable), i want to perform linq on the datatable to remove Invalid entries and move them in another datatable
datatable allEntries ( entries yet to be uploaded in database)
datatable InvalidCodes(single column datatable - retrieved from database)
datatable invalidEntries
right now "allEnties" contains valid entries and invalid entries. the linq query on "allEntries" should move the nonexistend code entries to invalidEntries datatable.
plz help me perform this.
below is the query i formed but its not valid
string query = "select [CityCode] from [StateCity] ";
DataTable citylist = getDataTableFromSelect(query);
var result = from myrow in inputDt.AsEnumerable()
where !myrow.Field<string>("CityCode").Contains(from myrow2 in citylist.AsEnumerable() select myrow2.Field<string>("CityCode") )
select myrow;
I'd make a HashSet for the invalid city codes - this will allow the code to quickly/efficiently identify which of the codes are in the invalid set.
e.g. something like:
var invalidCityCodes = from myrow2 in citylist.AsEnumerable()
select myrow2.Field<string>("CityCode");
var invalidCityCodeHashSet = new HashSet<string>(invalideCityCodes);
var result = from myrow in inputDt.AsEnumerable()
where !invalidCityCodeHashSet.Contains(myrow.Field<string>("CityCode"))
select myrow;
You can also take both the results in 2 Different Lists and then you can
use
List1 = List1.RemoveAll(Item=>List2.Contains(Item))
This works fine with me and will work for you also.

Resources