[英]How can I determine which properties exist/are filled in my database - and create rank - c#
I'm receiving products from a server and the response looks like this:我正在从服务器接收产品,响应如下所示:
{
"products": [
{
"id":"1",
"city": "New York",
"title": "Pepsi",
"brand": "Coca-Cola"
},
{
"id":"2",
"city": "Seatle",
"title": "Fanta",
"brand": "Idk"
},
{
"id":"3",
"city": "D.C W.",
"title": "Snickers",
"brand": "Fat"
}
]
}
I've previously also inserted some products in my Db... and I need to compare which properties already exist in my database.我之前还在我的数据库中插入了一些产品......我需要比较我的数据库中已经存在哪些属性。
The more properties are the persisted the higher the rank is.保留的属性越多,等级越高。
This "Rank" basically determines how well the data is synchronized.这个“等级”基本上决定了数据同步的好坏。
For example if two properties from a request exist in my database, that is rank 2, if all of them exist in my database that is rank 3 for instance.例如,如果请求中的两个属性存在于我的数据库中,即排名 2,如果它们都存在于我的数据库中,例如排名 3。
Here's what I tried:这是我尝试过的:
private IQueryable<ProductDto> OrderingProducts(IQueryable<Product> databaseProducts, RequestDto request)
{
foreach(var item in request.Products)
{ // Probably makes no sense.. :(
ord.AddRange(products.Where(c.Title == user.title && c.Brand == item.brand));
ord.AddRange(products.Where(c => c.City == item.city));
ord.AddRange(products.Where(c => c.Title == item.title ));
ord.AddRange(products.Where(c => c.Brand == item.brand));
}
}
Idea was to receive both lists, and to somehow compare which properties data exist in my database.想法是接收这两个列表,并以某种方式比较我的数据库中存在哪些属性数据。
I'm stuck on this, any kind of help would be awesome!我坚持这一点,任何形式的帮助都会很棒!
Something like this will get the ranking for you, but I don't know what type request.Products
is, or what you want to do with it afterwards.像这样的东西会为你获得排名,但我不知道
request.Products
是什么类型,或者你以后想用它做什么。
foreach (var item in request.Products)
{
var match = databaseProducts.SingleOrDefault(dp => dp.Id = item.Id);
int rank = match == null ? -1 : GetRank(item, match);
// TODO do something with rank
}
int GetRank(RequestTypeUnknown item, Product match)
{
return (item.city == match.City ? 1 : 0) +
(item.title == match.Title ? 1 : 0) +
(item.brand == match.Brand ? 1 : 0);
}
The response is in json format so you could deserialize it响应采用 json 格式,因此您可以对其进行反序列化
public class Response
{
public List<Product> Products { get; set; }
}
public class Product
{
public string Id { get; set; }
public string City { get; set; }
public string Title { get; set; }
public string Brand { get; set; }
}
Usage用法
Response res = JsonConvert.DeserializeObject<Response>(json);
foreach(var product in res.Products)
{
//product.Id
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.