Comments (9)
Hi @mateors. You are probably doing something wrong. You do a GroupBy over a list of maps. So in your keySelector function ShipmentTag
is a map - not a "ShipmentTag" key.
The thing that you probably want to do is:
data := From(rows).Where(func(c interface{}) bool {
cmap := c.(map[string]interface{})
return len(cmap["name"].(string)) > 0
}).GroupBy(func(c interface{}) interface{} {
cmap := c.(map[string]interface{})
return cmap["ShipmentTag"]
}, func(ShipmentTag interface{}) interface{} {
return ShipmentTag
}).First()
from go-linq.
@kalaninja Thank you very much, It works, But I am wondering How can I use ToMap() instead of First()
Could you please show me an example?
from go-linq.
result := make(map[string]interface{})
From(rows).WhereT(func(x map[string]interface{}) bool {
return len(x["name"].(string)) > 0
}).GroupByT(
func(x map[string]interface{}) interface{} {
return x["ShipmentTag"]
},
func(x interface{}) interface{} {
return x
},
).ToMapBy(&result,
func(i interface{}) interface{} {
return i.(Group).Key
}, func(i interface{}) interface{} {
return i.(Group).Group
})
from go-linq.
@kalaninja Thank you very much, you are superb! I noticed you used GroupByT but I want to use GroupBy only, no generics. I am concern about performance.
result := make(map[string]interface{})
data := From(rows).Where(func(c interface{}) bool {
cmap := c.(map[string]interface{})
return len(cmap["name"].(string)) > 0
}).GroupBy(func(c interface{}) interface{} {
cmap := c.(map[string]interface{})
return cmap["ShipmentTag"]
}, func(ShipmentTag interface{}) interface{} {
return ShipmentTag
}).ToMapBy(&result,
func(i interface{}) interface{} {
return i.(Group).Key
}, func(i interface{}) interface{} {
return i.(Group).Group
})
above code not working.
It says (no value) used as value
from go-linq.
that is probably because you are trying to read the resulting data from data
instead of result
?
ToMapBy
doesn't return a value, it populates a map you provide instead.
from go-linq.
@kalaninja No its compiling error, code seems okay but I don't know why not showing error. Could you please compile at your end? Advance thanks for your kind quick response.
from go-linq.
@mateors I think the reason might be because ToMapBy
doesn't return any value. Can you try removing data :=
from your code please?
from go-linq.
@kalaninja Thank you, following code working now. Jajakallah. You make my day. I am fasting as we are celebrating Ramadan, I pray for your success.
result := make(map[string]interface{})
From(rows).Where(func(c interface{}) bool {
cmap := c.(map[string]interface{})
return len(cmap["name"].(string)) > 0
}).GroupBy(func(c interface{}) interface{} {
cmap := c.(map[string]interface{})
return cmap["ShipmentTag"]
}, func(ShipmentTag interface{}) interface{} {
return ShipmentTag
}).ToMapBy(&result,
func(i interface{}) interface{} {
return i.(Group).Key
}, func(i interface{}) interface{} {
return i.(Group).Group
})
fmt.Println(result)
from go-linq.
I was glad to help @mateors. Welcome. And happy celebration.
from go-linq.
Related Issues (20)
- Append方法优化
- "GroupBy has better performance than GroupByT" is written on the document,why?
- ToChannelT release? HOT 2
- why union.go file doesn't have func UnionBy like the func IntersectBy in intersect.go
- Why `ToSlice` need a custom grow strategy instead of the standard library? HOT 1
- Unit test ExampleOrderedQuery_ThenByDescending fails on golang-1.19.2 HOT 6
- sort result is unexpected HOT 1
- How to implement the full join function of SQL
- new generic functions type HOT 1
- 转换错误 HOT 1
- I expect the data queried using gorm to be converted using go-linq HOT 1
- bug in WhereIndexed HOT 7
- Article comparing performance of go-linq vs others HOT 4
- gopkg.in/ahmetb/go-linq.v3: gopkg.in/ahmetb/[email protected]: parsing go.mod: module declares its path as: github.com/ahmetb/go-linq/v3 but was required as: gopkg.in/ahmetb/go-linq.v3 HOT 5
- how about add parameter timeout for FromChannelT?
- Try out the new Go generics proposal with go-linq HOT 14
- Is it safe to copy in multiple concurrency HOT 2
- How to implement complex SQL functions HOT 5
- PLINQ support HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-linq.