EF return columns data as list or array - sql-server

I have two tables, which have following foreign key
Poll(CreatedBy) --> CreateBy(ID)
On performing join, I am getting data as following:
I want to write an query using EF (if possible) to return result as:
[{
"id": 1,
"users": [
{
"email": "abc.com"
}
]
},
{
"id": 3,
"users": [
{
"email": "xyz.test"
},
{
"email": "tet.com"
}
]
}]

Assuming that the class structure like below:
public class Poll
{
public int ID { get; set; }
public string Name { get; set; }
public List<CreateBy> CreateBy { get; set; }
}
public class CreateBy
{
public int ID { get; set; }
public string EmailAddress { get; set; }
public string DisplayName { get; set; }
[ForeignKey("Poll")]
public int PollId { get; set; }
}
Data source:
Linq query:
var result = (from p in _context.Poll.ToList()
join c in _context.CreateBy on p.ID equals c.PollId
group p by p.ID into g
select new
{
id = g.Key,
users = g.Select(x => x.CreateBy.Where(z => z.PollId == g.Key).Select(y => new { email = y.EmailAddress })).FirstOrDefault()
}).ToList();
Result:

Related

Getting related data in .Net Core 3.1

I am trying to get related data from 2 tables. I am using .NET Core 3.1, EF Core, and the code-first approach. What I want is to pass a customerid (which is a foreign key in the order table) to get all the orders of that customer. I get some related output but I don't know how to get specific output.
I have commented out the .ThenInclude because it causes an error:
Lambda expression is not valid
Customer model class:
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace CRUDRelationship.Models
{
public class Customer
{
[Key]
public int CustomerId { get; set; }
public string Name { get; set; }
public string Email { get; set; }
//public List<Order> Order { get; set; }
}
}
Order model class:
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace CRUDRelationship.Models
{
public class Order
{
[Key]
public int OrderId { get; set; }
public int Total { get; set; }
[ForeignKey("Customer")]
public int CustomerId { get; set; }
public Customer customer { get; set; }
}
}
Interface:
using CRUDRelationship.Models;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace CRUDRelationship.Data
{
public interface ICustomerData
{
List<Customer> GetCustomers();
Customer GetCustomer(int id);
Customer AddCustomer(Customer customer);
List<Order> GetOrderByCustomer(int CustomerId);
Order AddOrder(Order order);
}
}
Implementation:
using CRUDRelationship.Models;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CRUDRelationship.Data
{
public class CustomerData : ICustomerData
{
private InvetoryContext _invetoryContext;
public CustomerData(InvetoryContext invetoryContext)
{
_invetoryContext = invetoryContext;
}
public Customer AddCustomer(Customer customer)
{
_invetoryContext.Customers.Add(customer);
_invetoryContext.SaveChanges();
return customer;
}
public Order AddOrder(Order order)
{
_invetoryContext.Orders.Add(order);
_invetoryContext.SaveChanges();
return order;
}
public Customer GetCustomer(int id)
{
throw new System.NotImplementedException();
}
public List<Customer> GetCustomers()
{
return _invetoryContext.Customers.ToList();
}
public List<Order> GetOrderByCustomer(int CustomerId)
{
return _invetoryContext.Orders
.Include(order => order.customer)
//.ThenInclude(customer => customer.Name)
.Where(order => order.CustomerId == CustomerId).ToList();
}
}
}
Customer table:
Order table:
Output I get when I pass customer id 2:
[
{
"orderId": 2,
"total": 200,
"customerId": 2,
"customer": {
"customerId": 2,
"name": "Ankit",
"email": "ankit#gmail.com"
}
},
{
"orderId": 4,
"total": 15000,
"customerId": 2,
"customer": {
"customerId": 2,
"name": "Ankit",
"email": "ankit#gmail.com"
}
}
]
This is the output I want:
{
"customer": {
"customerId": 2,
"name": "Ankit",
"email": "ankit#gmail.com",
"Order": [
{
"orderId": 2,
"total": 200,
"customerId": 2
},
{
"orderId": 4,
"total": 15000,
"customerId": 2
}
]
}
}

Dapper multi object query One-Many

Not really sure why I'm not getting the child object populated.
My tables:
Product:
[ProductId]
,[Brand]
,[Model]
StoreProduct:
[StoreId]
,[ProductId]
,[StoreProductId]
Class
public class Product
{
public Guid ProductId { get; set; }
public string Brand { get; set; }
public string Model { get; set; }
public virtual List<StoreProduct> StoreProducts { get; set; }
}
public class StoreProduct
{
public int StoreId { get; set; } //Key 0
public Guid ProductId { get; set; } //Key 1
public Store Store { get; set; }
public Product Product { get; set; }
public string StoreProductId { get; set; } //A new Id specific for each store
}
My Dapper Code
string sql = "SELECT * FROM StoreProduct AS A INNER JOIN Product AS B ON A.ProductId = B.ProductId WHERE A.StoreProductId = #StoreProductId and A.StoreId = #StoreId";
var connection = AppDbContext.Database.GetDbConnection();
return connection.Query<StoreProduct, Product, Product>(
sql,
(StoreProduct, Product) => { StoreProduct.ProductId = Product.ProductId; return Product; },
new { StoreProductId = storeProductId, StoreId = StoreID }, splitOn: "ProductId")
.FirstOrDefault();
What the DB returns:
But... StoreProducts List is null.
Use Dapper the way it works.
var listProduct = new Dictionary<string, Product>();
var listStoreProduct = new Dictionary<string, StoreProduct>();
using var connection = _connectionProvider.GetConnection();
var query = "SELECT * FROM StoreProduct AS A INNER JOIN Product AS B ON A.ProductId = B.ProductId WHERE A.StoreProductId = #StoreProductId and A.StoreId = #StoreId";
var result = connection.Query<Product, StoreProduct, Product>(query, (product, storeProduct) =>
{
if (!listProduct.TryGetValue(product.Id, out var entry))
{
entry = product;
listProduct.Add(entry.Id, entry);
}
if (storeProduct != null && !listStoreProduct.TryGetValue(storeProduct.Id, out var procInstance))
{
listStoreProduct.Add(procInstance.Id, procInstance);
entry.ProcessInstance.Add(procInstance);
}
return entry;
}, splitOn: "ProductId").Distinct().ToList();
I hope I could have helped you.

EF Core self joined ICollection query

I have a problem with querying data from a self reference datatable having an ICollection.
Here are my table structures:
public class SoqHeading
{
public int Id { get; set; }
public string ItemNo { get; set; }
public string Heading { get; set; }
public SoqRevision SoqRevision_NP { get; set; }
public ICollection<SoqItem> Items { get; set; }
public int? ParentSoqHeadingId { get; set; }
public SoqHeading Parent_NP { get; set; }
public ICollection<SoqHeading> Children_NP { get; set; }
}
public class SoqItem
{
public int Id { get; set; }
public SoqPreliminaryOrContract_enum PreliminaryOrContract { get; set; }
public int SoqHeadingId { get; set; }
public SoqHeading SoqHeading_NP { get; set; }
}
I have a self referenced relation in the SoqHeading table which is 1 to many. The SoqHeading table can have zero or many SoqItems.
This is how the data is in my database
what i want is to query all from the root node of the SoqHeading table with its children and the items.
like this
{
"ItemNo": "C",
"Items": [
{
"ItemNo": "c-1",
},
{
"ItemNo": "c-2",
}
],
"Children_NP": [
{
"ItemNo": "C.1",
"Children_NP": [
{
"ItemNo": "C.1.1",
"Items": [
{
"ItemNo": "c.1.1-1",
},
{
"ItemNo": "c.1.1-2",
}
],
"Children_NP": [],
},
{
"ItemNo": "C.1.2",
"Items": [
{
"ItemNo": "c.1.2-1",
},
{
"ItemNo": "c.1.2-2",
}
],
"Children_NP": [],
}
],
},
{
"ItemNo": "C.2",
"Children_NP": [],
}
]
}
But currently i get data from the query like this
[
{
"ItemNo": "C"
},
{
"ItemNo": "C.1.2",
}
]
where C.1.2 has to be inside C, but it not. C and C.1.2 are in the same level which i dont wont to have produced by the following query
var entity = await _context.SoqHeadings
.Include(i=>i.Items)
.Include(i => i.Children_NP)
.Where(w => w.SoqRevision_NP.ProjectId == 10)
.ToListAsync();
please help
so it seems to be EFcore has no support to included the children on the first child node https://github.com/dotnet/efcore/issues/14144
so the work around is something like this here https://khalidabuhakmeh.com/recursive-data-with-entity-framework-core-and-sql-server

Dapper Multi Mapping Result

I have two classes
public class Customer
{
public int CustomerId { get; set;}
public string CustomerName { get; set; }
}
public class Order
{
public int OrderId { get; set; }
public int CustomerId { get; set; } //BuyerCustomer
public int CustomerSecondId { get; set; } //ReceiverCustomer
public Customer BuyerCustomer { get; set; }
public Customer ReceiverCustomer { get; set; }
}
Here's my query will look like
SELECT a.*, b.*, c.* FROM dbo.PRODUCTS_ORDER a
INNER JOIN dbo.CUSTOMER b ON a.CustomerId=b.CustomerId
INNER JOIN dbo.CUSTOMER c ON a.CustomerSecondId=b.CustomerId
Dapper Implementation..
List<Order> order= null;
order= (List<Order>)dapperconnection.Query<Order, Customer, Customer, Order>(sql,
(order, customer1,customer2) =>
{
order.BuyerCustomer = customer1;
order.ReceiverCustomer = customer2;
return order;
}, splitOn: "CustomerId,CustomerSecondId ");
The result I'm getting is incomplete, only the RecevierCustomer gets populated while the BuyerCustomer doesn't contain any values at all.
It looks like dapper is confused since i used the CustomerId twice in my query.
Is there any workaround with this without having to change my the Customer class?
There are few issues with your class design and Dapper query.
Customer.CustomerName should be string
I would remove CustomerId and CustomerSecondId from Order. They are redundant. You have both Id's in the Customer.
Remove CustomerSecondId from Split.
Below is a working test:
public class Order
{
public int OrderId { get; set; }
public Customer BuyerCustomer { get; set; }
public Customer ReceiverCustomer { get; set; }
}
public class Customer
{
public int CustomerId { get; set; }
public string CustomerName { get; set; }
}
[Test]
public void TestSplitMany()
{
using (var conn = new SqlConnection(#"Data Source=.\sqlexpress;Integrated Security=true; Initial Catalog=foo"))
{
var result =
conn.Query<Order, Customer, Customer, Order>(#"select OrderId = 1, CustomerId = 2, CustomerName = 'Jim', CustomerId = 3, CustomerName = 'Bob'",
(order, buyer, receiver) =>
{
order.BuyerCustomer = buyer;
order.ReceiverCustomer = receiver;
return order;
}, splitOn: "CustomerId").First();
Assert.That(result.BuyerCustomer.CustomerId == 2);
Assert.That(result.ReceiverCustomer.CustomerId == 3);
Assert.That(result.BuyerCustomer.CustomerName == "Jim");
Assert.That(result.ReceiverCustomer.CustomerName == "Bob");
}
}

How insert new array into existing embedded array in C#?

I have a little bit problem with an data update in mongoDB. I have an array embedded in an other array who is embedded in a document (yaaaaay ^^). So my collection is like that :
{
"_id" : ObjectId("5965e0b4f1042c4c3f1d8edc"),
"Builder" : "Google",
"Family" : "Car",
"ProductName" : "ABCD_80A_PU170017",
"ProductNumber" : "N 1",
"Test" : [
{
"Num_Test" : "PU170017",
"Date_Start" : ISODate("2017-07-12T00:00:00.000Z"),
"Bench": [
{
"Room": 1,
"Num" : 1,
"Designation" : "AC1; AC3",
"IP" : "123.456.78.9",
"Capacity" : 12,
}
],
"User" : [
{
"Number" : "EBBW396",
"Password" : "titi",
"Name" : "NDJO",
"Surname" : "Charles",
"Tel" : "06XXXXXX",
"isAdmin" : true
}
]
}
]
}
And i want to insert a new array embedded in "Test".
So i found my happiness here : https://docs.mongodb.com/getting-started/csharp/update/ AND How to use C# to insert document into existing embedded documents?
And i have write this code :
static void Main(string[] args)
{
IMongoClient client = new MongoClient("mongodb://10.194.157.199:27017");
IMongoDatabase database = client.GetDatabase("PINEA");
IMongoCollection<Product> collection = database.GetCollection<Product>("Product");
var filter = Builders<Product>.Filter.Eq("Test.Num_Test", "PU170017");
var meter = new Meter();
meter.Value= 12;
meter.Date = DateTime.Now;
var meterList = new List<Meter>();
meterList.Add(meter);
var update = Builders<Product>.Update.Push("Result", new Result
{
MeterList = meterList
});
var result = collection.UpdateOne(filter, update);
}
But nothing happens, nothing has added or modified. I miss something important but i don't understand what is it.
Finally, here are my classes:
class Result
{
public List<Meter> MeterList { get; set; }
public List<Problem> Problem { get; set; }
}
class Meter
{
public int? Value { get; set; }
public DateTime? Date { get; set; }
}
class Test
{
public string Num_Test { get; set; }
public DateTime? Date_Start { get; set; }
public List<Bench> Bench { get; set; }
public List<User> User { get; set; }
public List<Result> Result { get; set; }
}
class Product
{
public string Builder { get; set; }
public string Fammily { get; set; }
public string ProductName { get; set; }
public string ProductNumber { get; set; }
public List<Test> Test { get; set; }
}
To explain the context, I need to update the array "result" regularly and maintain data arborescence like that. After the end of the test, data aren't modify anymore.
PS : I'm still a student but I work part time
Thanks

Resources