我有一个看起来像这样的动态对象,
{
"2" : "foo",
"5" : "bar",
"8" : "foobar"
}
Run Code Online (Sandbox Code Playgroud)
我该如何将其转换为dictionary
?
我试着这样做:
IEnumerable<object> ids = new List<string>() { "0001", "0002", "0003" };
Run Code Online (Sandbox Code Playgroud)
它很棒!
但是当我尝试这样做时:
IEnumerable<object> intIds = new List<System.Int32>() { 1, 2, 3 };
Run Code Online (Sandbox Code Playgroud)
Visual Studio告诉我:不能将类型'System.Collections.Generic.List'隐式转换为'System.Collections.Generic.IEnumerable'.存在显式转换(您是否错过了演员?)
这是为什么?
考虑以下代码:
switch (number)
{
case 1:
Number = (int)SmsStatusEnum.Sent;
break;
case 2:
Number = (int)SmsStatusEnum.Delivered;
break;
case 3:
Number = (int)SmsStatusEnum.Failed;
break;
default:
Number = (int)SmsStatusEnum.Failed;
break;
}
return Number;
Run Code Online (Sandbox Code Playgroud)
我有一个switch case
默认的.如果number
不是1,2或3的结果Failed
.所以我将代码转换为委托字典:
var statuses = new Dictionary<int, Func<SmsStatusEnum>>
{
{1,()=> SmsStatusEnum.Sent},
{2,()=> SmsStatusEnum.Delivered},
{3,()=> SmsStatusEnum.Failed},
};
Run Code Online (Sandbox Code Playgroud)
如何为委托字典模式设置默认值?
最近我在c#中发现了一个非常令人惊讶的行为.我有一个方法,它IEnumerable<Object>
作为参数,我正在通过,
IEnumerable<string>
但它是不可能的.虽然在c#中,所有内容都可以向上转换为对象而不是为什么这是不可能的?这让我很困惑.请有人在这个问题上告诉我.
昨天我遇到了一个问题 - findViewById()为我的工具栏返回NULL.我通过内部人员四处寻找,但似乎我无法找到解决我的"大"问题的方法:D
这是styles.xml
<resources>
<style name = "AppTheme" parent = "@style/Theme.AppCompat.NoActionBar">
<!-- TODO: Create AppTheme -->
<item name="android:windowActionBar">false</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
这是activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:tools = "http://schemas.android.com/tools"
android:id = "@+id/drawer_layout"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = ".Main" >
<LinearLayout
android:orientation="vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent" >
<android.support.v7.widget.Toolbar
android:id="@+id/tool_bar"
android:layout_width = "fill_parent"
android:layout_height = "@dimen/toolbar_height"
android:background = "@mipmap/bg_toolbar" >
<ImageView
android:id = "@+id/toolbar_drawer_button"
android:clickable="true"
android:layout_width = "wrap_content"
android:layout_height = "@dimen/toolbar_height"
android:src = "@mipmap/ic_drawer" …
Run Code Online (Sandbox Code Playgroud) 如果有一个csv文件,其数据会不时增加.现在我需要做的是阅读最后30,000行.
代码:
string[] lines = File.ReadAllLines(Filename).Where(r => r.ToString() != "").ToArray();
int count = lines.Count();
int loopCount = count > 30000 ? count - 30000 : 0;
for (int i = loopCount; i < lines.Count(); i++)
{
string[] columns = lines[i].Split(',');
orderList.Add(columns[2]);
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,但问题是
File.ReadAllLines(Filename)
Run Code Online (Sandbox Code Playgroud)
阅读导致性能不足的完整文件.我想要它只读取最后30,000行迭代整个文件.
PS:我正在使用.Net 3.5..Net 3.5中不存在Files.ReadLines()
AMS版本:0.9.7
我试图没有任何运气将参数传递给ActiveModel序列化器。
我的(浓缩)控制器:
class V1::WatchlistsController < ApplicationController
def index
currency = params[:currency]
@watchlists = Watchlist.belongs_to_user(current_user)
render json: @watchlists, each_serializer: WatchlistOnlySerializer
end
Run Code Online (Sandbox Code Playgroud)
我的序列化器:
class V1::WatchlistOnlySerializer < ActiveModel::Serializer
attributes :id, :name, :created_at, :market_value
attributes :id
def filter(keys)
keys = {} if object.active == false
keys
end
private
def market_value
# this is where I'm trying to pass the parameter
currency = "usd"
Balance.watchlist_market_value(self.id, currency)
end
Run Code Online (Sandbox Code Playgroud)
我正在尝试将参数currency
从控制器传递给要在market_value
方法中使用的序列化器(在示例中,其硬编码为“ usd”。
我已经尝试过@options和@instance_options,但似乎无法正常工作。不确定是否只是语法问题。
控制器接收 JSON 对象
{
user: {
name: "string",
details: {
info1: "string",
info2: []
}
}
}
Run Code Online (Sandbox Code Playgroud)
在权限控制器知道可以允许某些定义的字段(如名称)和具有所有嵌套属性的散列字段详细信息(也可以使用数组)。对于这种情况,正确的解决方案是什么?
糟糕的解决方案
permit
无法使用,因为我必须选择用户允许的字段
tap do |whitelisted|
无法使用,因为它不会使该字段“允许”
下面的情况不能是用户,因为使用数组不起作用
details_keys = params[:user][:details].keys
params.require(:user).permit(:name, details: details_keys)
我无法使用神rine为我的图像播种,与载波不同,以下代码不起作用。
Profile.create! id: 2,
user_id: 2,
brand: "The Revengers",
location: "Azgaurd",
phone_number: "send a raven",
image_data: File.open(Rails.root+"app/assets/images/seed/thor.png")
Run Code Online (Sandbox Code Playgroud)
我也尝试过
image_data: ImageUploader.new(:store).upload(File.open(Rails.root+"app/assets/images/seed/thor.png"))
Run Code Online (Sandbox Code Playgroud)
但它返回
JSON::ParserError in Profiles#show
743: unexpected token at '#<ImageUploader::UploadedFile:0x007fd8bc3142e0>'
Run Code Online (Sandbox Code Playgroud)
有圣地吗?我似乎在任何地方都找不到。
shrine.rb
require "cloudinary"
require "shrine/storage/cloudinary"
Cloudinary.config(
cloud_name: ENV['CLOUD_NAME'],
api_key:ENV['API_KEY'],
api_secret:ENV['API_SECRET'],
)
Shrine.storages = {
cache: Shrine::Storage::Cloudinary.new(prefix: "cache"), # for direct
uploads
store: Shrine::Storage::Cloudinary.new(prefix: "store"),
}
Run Code Online (Sandbox Code Playgroud)
profile.rb
class Profile < ApplicationRecord
include ImageUploader[:image]
belongs_to :user
has_and_belongs_to_many :genres
scoped_search on: [:brand]
end
Run Code Online (Sandbox Code Playgroud)
image_uploader.rb
class ImageUploader < Shrine
end
Run Code Online (Sandbox Code Playgroud) 我正在使用Bullet gem来查看应用程序中的n + 1个查询。它告诉我taggings
在调用序列化程序时急于加载我的关联。我的代码如下所示:
render json: @products, each_serializer: ::V1::ProductSerializer, includes: [:taggings], links: links, status: :ok
Run Code Online (Sandbox Code Playgroud)
但是添加完之后,我仍然从Bullet gem得到同样的警告。看起来像这样:
GET /api/v1/product_feed?state=CA&page=1
USE eager loading detected
Product => [:taggings]
Add to your finder: :includes => [:taggings]
Call stack
/home/jay/current_projects/api/app/controllers/api/v1/products_controller.rb:111:in `product_feed'
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么不急于加载标签表。
我有一个类,我将其称为SpiderNest
具有类型属性的类List<Spider>
,其中Spider
是一种具有类型属性的对象int
; 我们称之为这个属性NumberOfLegs
.我想要一个方法或SpiderNest
类的属性来获得我巢中所有蜘蛛的腿数总和.
是否首选使用这样的属性(原谅糟糕的对象命名):
class SpiderNest {
// Our example property.
public List<Spider> Spiders { get; set; }
public int TotalLegNumber
{
get { return Spiders.Sum(spider => spider.NumberOfLegs); }
}
Run Code Online (Sandbox Code Playgroud)
还是方法?
class SpiderNest {
public List<Spider> Spiders { get; set; }
public int GetTotalNumberOfLegs()
{
return Spiders.Sum(spider => spider.NumberOfLegs);
}
}
Run Code Online (Sandbox Code Playgroud)
你为什么选择这种方式?我知道这个问题可能很棘手,但每当我提出两种做事方式时,每种做事方式通常都会带来好处.谢谢!
c# ×5
covariance ×2
dictionary ×2
json ×2
.net-4.0 ×1
android ×1
attributes ×1
csv ×1
delegates ×1
dynamic ×1
file-io ×1
findviewbyid ×1
ienumerable ×1
json.net ×1
methods ×1
nested ×1
properties ×1
ruby ×1
shrine ×1
value-type ×1