Group newly made parameter values in linq to sql - asp.net

I am trying to group newly made key values but i'm not getting it to work.
This is the code
Dim result = From v In dc.tbl_voertuigs
Join tv In dc.tbl_type_voertuigs On tv.pk_type_voertuig Equals v.fk_type_voertuig
Join tbc In dc.tbl_tebehalencertificaats On tbc.pk_tebehalencertificaat Equals v.fk_tebehalencertificaat
Where tbc.naam_certificaat = subquery2
Select New Voertuig With
{
.p_voertuigid = v.fk_type_voertuig,
.p_voertuigomschr = tv.type + " -- " + tv.merk
}
I would like to group on .p_voertuigid. I already tried some options without any result. I hope someone here can help me.
Thanks in advance!
I think i am almost there, but now i'm getting a new exception:
Value of type system.collections.generic.list(of <anonymoustype>) cannot be converted
to system.collections.generic.list(of tbl_voertuig)
This is my new code
Dim result = (From v In dc.tbl_voertuigs
Join tv In dc.tbl_type_voertuigs On tv.pk_type_voertuig Equals v.fk_type_voertuig
Join tbc In dc.tbl_tebehalencertificaats On tbc.pk_tebehalencertificaat Equals v.fk_tebehalencertificaat
Where tbc.naam_certificaat = subquery2
Group By v = New Voertuig With
{
.p_voertuigid = v.fk_type_voertuig,
.p_voertuigomschr = tv.type + " -- " + tv.merk
} Into grp = Group).ToList
Return result

I found the answer, this is the final code and it works
Dim result = From v In dc.tbl_voertuigs
Join tv In dc.tbl_type_voertuigs On tv.pk_type_voertuig Equals v.fk_type_voertuig
Join tbc In dc.tbl_tebehalencertificaats On tbc.pk_tebehalencertificaat Equals v.fk_tebehalencertificaat
Where tbc.naam_certificaat = subquery2
Group By v = New Voertuig With
{
.p_voertuigid = v.fk_type_voertuig,
.p_voertuigomschr = tv.type + " -- " + tv.merk
} Into grp = Group _
Select v
Return result.ToList
I hope this can help someone who has the same problem in the future
Grtz
Yannick

Related

Count resulted records using linq to sql

i use linq to sql query for retrive records from database.
i use a query,for binding a gridview.
protected void grdBind()
{
try
{
EventManagerDataContext db = new EventManagerDataContext();
var z = (from x in db.EMR_EVENTs
join y in db.EMR_CLIENTs on x.ClientID equals y.ClientID
where y.ClientID==x.EventID
select x.EventID).Count();
var q = from a in db.EMR_CLIENTs
join b in db.EMR_EVENTs on a.ClientID equals b.ClientID
join c in db.EMR_ACCOUNTs on a.ClientID equals c.ClientID
join d in db.EMR_SUBSCRIPTIONs on c.Account_ID equals d.Account_ID
join f in db.EMR_SUBSCRIPTION_KINDs on d.Subscription_kind_ID equals f.Subscription_kind_ID
select new
{
Customer = a.Name,
Events = z,
TurnOver = f.Monthly_Fee,
StartDate = d.Start_Date,
EndDate = d.End_Date,
CreteDate = d.Create_Date,
ClientID = a.ClientID,
EventID = b.EventID,
SubscriptionID = d.Subscription_ID,
Subscription_kind_ID=f.Subscription_kind_ID,
Account_ID=c.Account_ID,
};
grid.DataSource = q.ToList();
grid.PageSize = int.Parse(drpPageSize.SelectedValue);
grid.DataBind();
}
catch
{
throw;
}
}
and i recieve this output for that,
i recieve this output for this query but i don't want this output ,
i want like this output.
clientname events
ketan 18
monika 12
and others records so on means i recieve here client name 9 times and he created events but i want some of events and client name only one time
means i want only one name of client and total number of events,i am new to linq to sql.
so what is the changes in code..?
when you are using join syntax in query you do not need to use 'where'
then change your query to :
var z = (from x in db.EMR_EVENTs
join y in db.EMR_CLIENTs on x.ClientID equals y.ClientID
select x.EventID).Count();
i found solution .here
ans also use with this.useful link
here is my solution.
EventManagerDataContext db = new EventManagerDataContext();
var q = from a in db.EMR_CLIENTs
join b in db.EMR_EVENTs on a.ClientID equals b.ClientID into z
join c in db.EMR_ACCOUNTs on a.ClientID equals c.ClientID
join d in db.EMR_SUBSCRIPTIONs on c.Account_ID equals d.Account_ID
join f in db.EMR_SUBSCRIPTION_KINDs on d.Subscription_kind_ID equals f.Subscription_kind_ID
select new
{
Customer = a.Name,
Events =z.Where(b =>b.ClientID==a.ClientID).Count(),
TurnOver = f.Monthly_Fee,
StartDate = d.Start_Date,
EndDate = d.End_Date,
CreteDate = d.Create_Date,
ClientID = a.ClientID,
SubscriptionID = d.Subscription_ID,
Subscription_kind_ID=f.Subscription_kind_ID,
Account_ID=c.Account_ID,
};
grid.DataSource = q.ToList();

There is no row at position 0 error

I have the following code:
Do Until i = dsSalesRep.Tables(0).Rows.Count
strRepID = dsSalesRep.Tables(0).Rows(i)("repID").ToString
strLBP = dsSalesRep.Tables(0).Rows(i)("officename").ToString
strLBPEmail = dsSalesRep.Tables(0).Rows(i)("cemail").ToString
strCCRepEmail = dsSalesRep.Tables(0).Rows(i)("cc1").ToString
strRepName = dsSalesRep.Tables(0).Rows(i)("firstname").ToString & " " & dsSalesRep.Tables(0).Rows(i)("lastname").ToString
strRepEmail = dsSalesRep.Tables(0).Rows(i)("email").ToString
strRepPhone = dsSalesRep.Tables(0).Rows(i)("phone").ToString
searchStr3 = "SELECT * FROM LookupSalesRep WHERE repID='" & strRepID & "'"
Dim SqlAdapter3 As New SqlDataAdapter(searchStr3, myConn2)
Dim dsTerritories As New DataSet
myConn2.Open()
SqlAdapter3.Fill(dsTerritories)
strCountry = dsTerritories.Tables(0).Rows(0)("country").ToString
'strCountry = dsTerritories.Tables(0).Rows.Count.ToString
When I run the web page, though, it gives me the error: There is no row at position 0.
But when I use the code
strCountry = dsTerritories.Tables(0).Rows.Count.ToString
strCountry gets the right number of rows. Please help :(
You are in a loop and at some point the table (dsTerritories.Tables(0)) is null. Check first if the table is null:
If dsTerritories.Tables(0).Rows.Count > 0 Then
strCountry = dsTerritories.Tables(0).Rows(0)("country").ToString
End If

Tridion Filter : replacement for SetCustomMetaQuery

What is the replacement for SetCustomMetaQuery in Broker query Mechanism (Tridion 2011)?
I got lots of help through this post posted by me earlier.
for
query.SetCustomMetaQuery("KEY_NAME='Key' AND KEY_STRING_VALUE >= 'Yes'");
I tried
CustomMetaValueCriteria criteria1 = new CustomMetaValueCriteria(new CustomMetaKeyCriteria("Key"), "Yes");
mainCriteria =CriteriaFactory.And(mainCriteria, criteria1);
query.Criteria = mainCriteria;
But I am stuck at below two examples in one of the filter CTs.
query.SetCustomMetaQuery("(((KEY_NAME='EventStartDate' AND
KEY_DATE_VALUE >= '" + lowerDate + "')) or
((KEY_NAME='EventEndDate' AND KEY_DATE_VALUE >= '" + lowerDate + "')))"")
and
query.SetCustomMetaQuery("KEY_NAME = 'Publication_Issue_Date' and
((convert(varchar(10), key_date_value, 101) = convert(varchar(10),
cast('" + sIssueDate + "' as datetime), 101)) or
key_string_value like '%" + dtIssue[%=nNumber%].Year + "-0" + dtIssue[%=nNumber%].Month + "-" + dDay[%=nNumber%] + "%')");
Could any please help me with this?
Thought I'd have a stab at this so could be way off!
How's this for the first example:
CustomMetaValueCriteria criteria1 = new CustomMetaValueCriteria(new CustomMetaKeyCriteria("EventStartDate"), lowerDate, Criteria.GreaterThanOrEqual);
CustomMetaValueCriteria criteria2 = new CustomMetaValueCriteria(new CustomMetaKeyCriteria("EventEndDate"), lowerDate, Criteria.GreaterThanOrEqual);
OrCriteria or = new OrCriteria(criteria1, criteria2);
Assumes that "lowerDate" is a DateTime.
The second one is a bit confusing, it looks like its checking for the existence of "Publication_Issue_Date" and any KEY_DATE_VALE that equals the sIssueDate variable OR that there's a string metadata value in the from of a date. So is that going to be something like?
CustomMetaKeyCriteria criteria1 = new CustomMetaKeyCriteria ("Publication_Issue_Date")
CustomMetaValueCriteria criteria2 = new CustomMetaValueCriteria(sIssueDate);
AndCriteria andCriteria = new AndCriteria(criteria1, criteria2);
string dt = dtIssue[%=nNumber%].Year + "-0" + dtIssue[%=nNumber%].Month + "-" + dDay[%=nNumber%];
CustomMetaValueCriteria criteria3 = new CustomMetaValueCriteria(dt, Criteria.Like)
OrCriteria or = new OrCriteria(andCriteria, criteria3);
Cheers
Please try with:
CustomMetaValueCriteria criteria1 = new CustomMetaValueCriteria(new CustomMetaKeyCriteria("Key"), "Yes", Criteria.GreaterThanOrEqual);
Let me know if this works.

Entity Framework Navigable Property Not Set to Instance of Object?

I have the following query:
Dim queryStudent = (From p In dbContext.Residents _
Where p.people_code_id = people_id _
Where p.year = year _
Where p.semester = semester _
Join b In dbContext.Buildings On p.building Equals b.id _
Join r In dbContext.Rooms On p.room Equals r.id
Select p, b, r)
I then attempt to pull the building and room for that individual like so:
room = queryStudent.FirstOrDefault.r.id
building = queryStudent.FirstOrDefault.b.id
But I receive an Object reference not set to an instance of an object error.
I tried doing something like
If IsNothing(queryStudent.FirstOrDefault.r.id) Then
room = ""
Else
room = queryStudent.FirstOrDefault.r.id
End If
But that still generates the same error.
Check if r is nothing instead of the id of r.
If IsNothing(queryStudent.FirstOrDefault.r) Then
room = ""
Else
room = queryStudent.FirstOrDefault.r.id
End If
I suspect queryStudent.FirstOrDefault is null. If you try to access a property of a null object, you get that exception. Try this approach:
If IsNothing(queryStudent.FirstOrDefault) Then
room = ""
Else If IsNothing(queryStudent.First.r) Then
room = ""
Else
room = queryStudent.FirstOrDefault.r.id
End If
In each case, you are checking that the object you are about to access is not null.

ASP.Net String Split not working

Here's my code
Dim RefsUpdate As String() = Session("Refs").Split("-"C)
Dim PaymentsPassedUpdate As String() = Session("PaymentsPassed").Split("-"C)
Dim x as Integer
For x = 1 to RefsUpdate.Length - 1
Dim LogData2 As sterm.markdata = New sterm.markdata()
Dim queryUpdatePaymentFlags as String = ("UPDATE OPENQUERY (db,'SELECT * FROM table WHERE ref = ''"+ RefsUpdate(x) +"'' AND bookno = ''"+ Session("number") +"'' ') SET alpaid = '"+PaymentsPassedUpdate(x) +"', paidfl = 'Y', amountdue = '0' ")
Dim drSetUpdatePaymentFlags As DataSet = Data.Blah(queryUpdatePaymentFlags)
Next
I don't get any errors for this but it doesn't seem to working as it should
I'm passing a bookingref like this AA123456 - BB123456 - CC123456 - etc and payment like this 50000 - 10000 - 30000 -
I basically need to update the db with the ref AA123456 so the alpaid field has 50000 in it.
Can't seem to get it to work
Any ideas?
Thanks
Jamie
I'm not sure what isn't working, but I can tell you that you are not going to process the last entry in your arrays. You are going from 1 to Length - 1, which is one short of the last index. Therefore, unless your input strings end with "-", you will miss the last one.
Your indexing problem mentioned by Mark is only one item, but it will cause an issue. I'd say looking at the base your problem stems from not having trimmed the strings. Your data base probably doesn't have spaces leading or trailing your data so you'll need to do something like:
Dim refsUpdateString as string = RefsUpdate(x).Trim()
Dim paymentsPassedUpdateString as string = PaymentsPassedUpdate(x).Trim()
...
Dim queryUpdatePaymentFlags as String = ("UPDATE OPENQUERY (db,'SELECT * FROM table WHERE ref = ''" & refsUpdateString & "'' AND bookno = ''" & Session("number") & "'' ') SET alpaid = '" & paymentsPassedUpdateString & "', paidfl = 'Y', amountdue = '0' ")
Also, I would recommend keeping with the VB way of concatenation and use the & character to do it.

Resources