Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Selecting a random content record
PurpleSky
#1 Posted : Sunday, March 28, 2010 4:37:38 AM(UTC)
Rank: Member
Groups: Registered

Joined: 1/1/2010(UTC)
Posts: 11
Location: Hampshire, UK
Is there a way of selecting a random content record.

What I want to do is select single record from a list of testimonials.

Jen
zguoqi
#2 Posted : Sunday, March 28, 2010 11:57:53 AM(UTC)
Rank: Administration
Groups: Registered, Administrators

Joined: 9/2/2009(UTC)
Posts: 690
Location: xiamen
Jen,

There is an easy way to do this.

In the data rule definition, you can put "NewId()" in the Order By column, you can see this column when you open the advanced setting of datarule.

This will produce the SQL like "SELECT * FROM TABLE ORDER BY NewId()", Newid() will generate a new GUID which is also random, when you order by this new GUID, you can produce a random like selection.


Regards,

Vincent

Kooboo Team
PurpleSky
#4 Posted : Sunday, March 28, 2010 10:00:32 PM(UTC)
Rank: Member
Groups: Registered

Joined: 1/1/2010(UTC)
Posts: 11
Location: Hampshire, UK
Great thanks for that adding a TOP 1 into the select and that worked great.

Jen
weedweaver
#3 Posted : Thursday, June 23, 2011 9:59:26 AM(UTC)
Rank: Member
Groups: Registered

Joined: 6/23/2011(UTC)
Posts: 19
Location: Switzerland
zguoqi;1611 wrote:
Jen,

There is an easy way to do this.

In the data rule definition, you can put "NewId()" in the Order By column, you can see this column when you open the advanced setting of datarule.

This will produce the SQL like "SELECT * FROM TABLE ORDER BY NewId()", Newid() will generate a new GUID which is also random, when you order by this new GUID, you can produce a random like selection.




Is this not possible in the latest version? I have a drop down list which doesn't contain NewId() in the list.

Thanks
weedweaver
#5 Posted : Thursday, June 23, 2011 11:05:24 AM(UTC)
Rank: Member
Groups: Registered

Joined: 6/23/2011(UTC)
Posts: 19
Location: Switzerland
p.s. the text says "Sort filed", this maybe a typo, should it be "Sort Field"?

Thanks
NjayWu
#6 Posted : Thursday, June 23, 2011 3:14:58 PM(UTC)
Rank: Advanced Member
Groups: Registered

Joined: 3/14/2011(UTC)
Posts: 34
Location: XM
Why not use Content Query API.The API is very easy and likes LINQ in Kooboo3.

@{
var result = ContentHelper.TextFolder("Article").CreateQuery().OrderBy(it=>Guid.NewGuid()).FirstOrDefault();
}
Regards,

N.J Wu

Kooboo3 Fans
hanoivatoi_1985
#8 Posted : Sunday, August 14, 2011 3:17:39 PM(UTC)
Rank: Advanced Member
Groups: Registered

Joined: 3/19/2011(UTC)
Posts: 114
Location: Mercedes C200
subjects such as product attributes are ID, Name, Price, Currency.

linq:

var newList = from p in productList where ((p.Price <100000 and p.Currency = 2) or(p .Price <100000/20000 and p.Currency = 1)) select p;


var result = ContentHelper.TextFolder("productList").CreateQuery().Where((it=> it.Price <100000) or (it=> it.Currency = 2)).FirstOrDefault();
--> Error

var check = ContentHelper.TextFolder("productList").CreateQuery().WhereEquals("Currency", "1").WhereLessThan("Price", "100000");
var check1 = ContentHelper.TextFolder("productList").CreateQuery().WhereEquals("Currency", "2").WhereLessThan("Price", "30");

how to use "or" to check2 = check + check1

Error.
you can rewrite the code on the API KooBoo + linq not help yourself?
jifeng
#9 Posted : Monday, August 15, 2011 8:00:56 AM(UTC)
Rank: Administration
Groups: Administrators, Registered

Joined: 9/3/2009(UTC)
Posts: 1,394
Location: Xiamen China
@using Kooboo.CMS.Content.Query.Expressions;
@{
var exp1 = new AndAlsoExpression(new WhereEqualsExpression(null, "Current", "1"), new WhereLessThanExpression(null, "Price", "100000"));
var exp2 = new AndAlsoExpression(new WhereEqualsExpression(null, "Current", "2"), new WhereLessThanExpression(null, "Price", "30"));
var exp3 = new OrElseExpression(exp1, exp2);
var contents = ContentHelper.TextFolder("productList").CreateQuery().Where(exp3);

}
Regards,

Jifeng Huang

Kooboo Team

Microsoft ASP.NET MVP
2 users thanked jifeng for this useful post.
Baseless on 8/15/2011(UTC), hanoivatoi_1985 on 8/15/2011(UTC)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF 1.9.5.5 | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.140 seconds.