Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some usefull extensions that helps write code faster #713

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nester-a
Copy link

Hi
Added a few methods that speed up development using SqlKata.

@ahmad-moussawi
Copy link
Contributor

@nester-a could you provide more details about this PR?

@nester-a
Copy link
Author

@nester-a could you provide more details about this PR?

I've added three methods.

1 is Select<T>() that allows you to not pass a list of columns to be fetched.
Ex:

class Post
{
    public int Id { get; set; }
    public string Title { get; set; }
    public Date CreatedAt  { get; set; }
}

//before
new Query("Posts").Select("Id", "Title", "CreatedAt").Get<Post>();

//method I added
new Query("Posts").Select<Post>().Get<Post>();

2 and 3 are Insert<T>() and it async version InsertAsync<T>() that allows you do not pass enumerable of columns and values then you need insert many rows
Ex:

class Product
{
    public string Name { get; set; }
    public decimal Price{ get; set; }
}

//before
var cols = new [] {"Name", "Price"};

var data = new [] {
    new object[] { "A", 1000 },
    new object[] { "B", 2000 },
    new object[] { "C", 3000 },
};

db.Query("Products").Insert(cols, data);

//method I added
var data = new List<Product>()
{
    new(){ Name = "A",  Price = 1000 },
    new() { Name = "B",  Price = 2000 },
    new(){ Name = "C",  Price = 3000 }
}

db.Query("Products").Insert(data);

Select<T>() and Insert<T>() use reflection to derive property names and use them as column names

@fairking
Copy link

fairking commented Apr 25, 2024

@nester-a Do you use data annotations for column and table names? https://stackoverflow.com/questions/56993920/reflect-table-column-names-based-on-data-annotation

Sometimes I use such annotation when my columns do not reflect the reality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants