如何确定数据库中存在/填充了哪些属性-并创建秩-c#
我正在从服务器接收产品,响应如下所示:如何确定数据库中存在/填充了哪些属性-并创建秩-c#,c#,C#,我正在从服务器接收产品,响应如下所示: { "products": [ { "id":"1", "city": "New York", "title": "Pepsi", "brand": "Coca-Cola" }, { "id":"2", "city": "Seatle",
{
"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"
}
]
}
我以前也在我的数据库中插入了一些产品。。。我需要比较数据库中已经存在的属性
持久化的属性越多,级别越高
这个“排名”基本上决定了数据的同步程度
例如,如果我的数据库中存在一个请求的两个属性,即秩2,
如果它们都存在于我的数据库中,例如排名3的数据库
以下是我尝试过的:
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));
}
}
专用IQueryable OrderingProducts(IQueryable数据库产品,请求到请求)
{
foreach(request.Products中的var项)
{//可能没有意义……:(
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));
}
}
想法是接收两个列表,并以某种方式比较数据库中存在哪些属性数据
我被困在这个问题上,任何形式的帮助都会很棒!类似的东西会为您获得排名,但我不知道request.Products是什么类型,或者您以后想用它做什么
foreach(request.Products中的var项)
{
var match=databaseProducts.SingleOrDefault(dp=>dp.Id=item.Id);
int rank=match==null?-1:GetRank(项目,匹配);
//做些有地位的事
}
int GetRank(请求类型未知项,产品匹配)
{
返回(item.city==match.city?1:0)+
(item.title==match.title?1:0)+
(item.brand==匹配品牌?1:0);
}
像这样的东西会让你获得排名,但我不知道request.Products是什么类型的,或者你以后想用它做什么
foreach(request.Products中的var项)
{
var match=databaseProducts.SingleOrDefault(dp=>dp.Id=item.Id);
int rank=match==null?-1:GetRank(项目,匹配);
//做些有地位的事
}
int GetRank(请求类型未知项,产品匹配)
{
返回(item.city==match.city?1:0)+
(item.title==match.title?1:0)+
(item.brand==匹配品牌?1:0);
}
响应是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; }
}
公共类响应
{
公共列表产品{get;set;}
}
公共类产品
{
公共字符串Id{get;set;}
公共字符串City{get;set;}
公共字符串标题{get;set;}
公共字符串品牌{get;set;}
}
用法
Response res=JsonConvert.DeserializeObject(json);
foreach(实物产品中的var产品)
{
//产品Id
}
响应是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; }
}
公共类响应
{
公共列表产品{get;set;}
}
公共类产品
{
公共字符串Id{get;set;}
公共字符串City{get;set;}
公共字符串标题{get;set;}
公共字符串品牌{get;set;}
}
用法
Response res=JsonConvert.DeserializeObject(json);
foreach(实物产品中的var产品)
{
//产品Id
}
我假设产品
应该是数据库产品
?我假设产品
应该是数据库产品
?我喜欢这样。为了让事情更清楚一点,我想我应该在GetRank
函数中使用三元运算符,而不是转换为int32。返回(item.City==match.City?1:0)+…
。只是为了提高可读性。我喜欢这样。为了让事情更清楚一点,我想我应该在GetRank
函数中使用三元运算符,而不是转换为int32。返回(item.City==match.City?1:0)+…
。只是为了帮助提高可读性。我们可以假设RequestDto
是JSON的反序列化形式。@SamAxe好的,这个问题最初似乎不知道如何访问属性值。谢谢。我们可以假设RequestDto
是JSON的反序列化形式。@SamAxe好的,这个问题很好一开始似乎不知道如何获取房产价值。谢谢。