简体   繁体   English

如何确定我的数据库中存在/填充了哪些属性 - 并创建排名 - c#

[英]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.

相关问题 如何使用C#确定本地网络上的哪些IP地址是静态/动态的? - How can I use C# to determine which IP addresses on my local network are static/dynamic? 如何确定我的 C# 应用程序运行的“位”? - How can I determine the "bit-ness" under which my C# application runs? c# Winforms中不存在时如何创建数据库 - How to create database if not exist in c# Winforms 如何创建可以使用C#选择字体的组合框? - How can i create combo boxes which i can choose my font with C#? 以编程方式,如何指定要在其中放置表 C# 的数据库 - Programmatically, how can I specify the database in which I want to put my table C# 为什么我不能在C#数据库中创建表? - Why I can't create a table in my database in C#? 如何使用c#中的List创建替代数据库? - How can I create my alternative database using List in c#? C# 如何访问实现接口中不存在的 object 属性 - C# how can I access the object properties that not exist in the implemented interface 如何播放参考资料中的Flash电影? (C#) - How can I play a flashmovie which is in my references? (C#) 如果表不存在,如何在数据库中创建新表(C#) - How can create a new table in database if the table does not exist (c#)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM