Coder Social home page Coder Social logo

Comments (9)

kalaninja avatar kalaninja commented on June 20, 2024

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.

mateors avatar mateors commented on June 20, 2024

@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.

kalaninja avatar kalaninja commented on June 20, 2024

@mateors

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.

mateors avatar mateors commented on June 20, 2024

@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.

kalaninja avatar kalaninja commented on June 20, 2024

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.

mateors avatar mateors commented on June 20, 2024

@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.

kalaninja avatar kalaninja commented on June 20, 2024

@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.

mateors avatar mateors commented on June 20, 2024

@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.

kalaninja avatar kalaninja commented on June 20, 2024

I was glad to help @mateors. Welcome. And happy celebration.

from go-linq.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.