我有一本字典
Dictionary<string, string> rList = new Dictionary<string, string>();
rList .Add("/a/b/c", "35");
rList .Add("/a/c/f/v", "25");
rList .Add("/a/r/d/c/r/v", "29");
rList .Add("/a", "21");
rList .Add("/a/f, "84");
Run Code Online (Sandbox Code Playgroud)
我只想根据密钥中存在的"/"数来对此字典进行排序.我的预期出局是,
("/a/r/d/c/r/v", "29")
("/a/c/f/v", "25")
("/a/b/c", "35")
("/a/f, "84")
("/a", "21")
Run Code Online (Sandbox Code Playgroud) 下面的 For 循环根本不循环。在没有 For 循环的情况下,是否有任何优化的方法可以做到这一点:
For Each drID As DataRow In dttable.Select("ID=1 and FirstName='Karthik'", "ID")
NewID = CInt(drID.Item("ID"))
Exit For
Next
Run Code Online (Sandbox Code Playgroud)
我试过用
NewID = IIf(dt.Select("ID=1 and FirstName='Karthik'", "ID").Length > 0, dt.Select("ID=1 and FirstName='Karthik'", "ID")(0).Item("ID"), 0)
Run Code Online (Sandbox Code Playgroud)
有没有其他优化的方法来改变这个根本不循环的 For 循环。
我看到了很多重复的帖子但对我来说这是不同的东西.
我有一个Datetime对象并获取时间部分并为其分配另一个时间.当我要分配它时,它会引发这些错误.
在这里newStartDateGroup是一个DateTime对象,这里的OpenTime是一个TimeSpan
无法将属性或索引器分配给 - 它是只读的
else if(newStartDateGroup.TimeOfDay < i.OpenTime && newEndDateGroup.TimeOfDay > i.CloseTime) // < >
{
newStartDateGroup.TimeOfDay = i.OpenTime;
Run Code Online (Sandbox Code Playgroud) 如何将此转换为使用Linq到数据集?
foreach (Row row in Rows)
{
if (row.material> 0)
sumtid += row.time * row.material;
else
sumtid += row.time;
}
Run Code Online (Sandbox Code Playgroud) 使用下面的DateTime.TryPaseExact方法可以在预期的时候给出不同的输出.
我从以下代码返回以下格式:
2014-02-10 18:32:37
1402-10-18 17:23:00 (which is clearly incorrect)
Run Code Online (Sandbox Code Playgroud)
我如何才能获得yyyy-MM-dd HH:mm:ss返回的格式?
static readonly string[] DateFormats = new string[] { "yyMMddHHmm", "yyyyMMddHHmm", "yyMMddHHmmss", "yyyy-MM-dd HH:mm:ss" };
DateTime date;
DateTime.TryParseExact("140211090915", DateFormats, CultureInfo.InvariantCulture,
DateTimeStyles.AssumeLocal, out date);
Run Code Online (Sandbox Code Playgroud)
此外,如果有人可以解释为什么此140211084947输入返回预期的格式并140211090915返回不需要的格式,我将不胜感激
我想找到字符串中字符的第二高频率.例如:abccddd
O/p应该是:c
我试着用字典.我将所有内容存储在字典中.而且,我也在整理它.现在我不知道该怎么办.
string input = tb_input.Text;
Dictionary<string, int> di = new Dictionary<string, int>();
for (int i = 0; i < input.Length; i++)
{
if (di.ContainsKey(input[i].ToString()))
{
int value = di[input[i].ToString()];
value++;
di[input[i].ToString()] = value;
}
else
{
di.Add(input[i].ToString(), 0);
}
}
var items = di.Values.ToList();
items.OrderByDescending(x => x).ToList();
Run Code Online (Sandbox Code Playgroud) 我写了一个小方法来列出继承的类型,但它不适TreeNode用于例如:
假设这个类:
class B { }
class A : B { }
class C :TreeNode { }
Run Code Online (Sandbox Code Playgroud)
然后:
GetInheritedTypes(typeof(A)); //typeof(B)
GetInheritedTypes(typeof(C)); // 0 items
Run Code Online (Sandbox Code Playgroud)
列出它们的方法:
List<Type> GetInheritedTypes(Type baseType)
{
return Assembly.GetAssembly(baseType)
.GetTypes()
.Where(type => type != baseType && type.IsAssignableFrom(baseType))
.ToList();
}
Run Code Online (Sandbox Code Playgroud)
为什么GetInheritedTypes(typeof(C))返回0项而不是Typeof(TreeNode)?
字符串格式
addr,hname,beam,txrate,rxrate,dcap,ucap,txuse,rxuse,rxrssi0,rxrssi1,txrssi0,txrssi1,txper,rxper,txopqual
04:18:D6:bb:F4:C6,Name,0,270000,270000,295650,263250,31,17,35,36,37,35,124,229,0
Run Code Online (Sandbox Code Playgroud)
期望的输出
addr = 04:18:D6:bb:F4:C6
hname = Name
beam = 0
Run Code Online (Sandbox Code Playgroud)
等等... ...我想在t键值中配对,但键是一个新的行形式值并将它们放入一个字典输出使用此代码可以工作,但我想知道是否有一种更有效的方法,可以跳过空值
这就是我到目前为止所拥有的
Dictionary<string, string> INFO = new Dictionary<string, string>();
var terminal = client.RunCommand("amstainfo");
var output = terminal.Result;
string[] line = output.Split(new string[] { "\n" }, StringSplitOptions.None);
string[] KEY = line[0].Split(new string[] { "," }, StringSplitOptions.None);
string[] VALUE = line[1].Split(new string[] { "," }, StringSplitOptions.None);
int i = 0;
foreach (var ist in KEY)
{
INFO.Add(KEY[i], VALUE[i]);
i++;
}
Run Code Online (Sandbox Code Playgroud) 我正在使用 C# 和 Linq 编写一个 XML 文件,但是在尝试保存它时,它给了我“状态文档中的令牌 EndDocument 会导致无效的 XML 文档”错误。创建和保存文档的代码:
XDocument xDoc = new XDocument();
using (var db = new CarRentalEntities1())
{
foreach (Car c in db.Cars)
{
XElement root = new XElement("root",
new XElement
(
"Car-" + c.CarName,
new XAttribute("CarID", c.CarID),
new XAttribute("CarName", c.CarName),
new XAttribute("CarType", c.CarType),
new XAttribute("Reg", c.Registration),
new XAttribute("YearOfPurchase", c.YearOfPurchase)
)
);
}
xDoc.Save("D:\\Cars.XML");
}
Run Code Online (Sandbox Code Playgroud) 我想在LINQ选择中将日期和时间转换为波斯语,但linq无法识别我的方法:
LINQ to Entities无法识别方法'System.String toPersianDateTime(System.DateTime)'方法,并且此方法无法转换为存储表达式.
如何将我的方法更改为LINQ兼容?
我的方法:
public static string toPersianDateTime(DateTime dt)
{
PersianCalendar pc = new PersianCalendar();
string pDateTime = pc.GetYear(dt).ToString() + "/" + pc.GetMonth(dt).ToString() + "/" + pc.GetDayOfMonth(dt).ToString() + " ";
pDateTime += pc.GetHour(dt) + ":" + pc.GetMinute(dt) + ":" + pc.GetSecond(dt);
return pDateTime;
}
Run Code Online (Sandbox Code Playgroud)
我的LINQ代码:
var result = (from ord in db.vw_orders
where ord.uid == user.id
orderby ord.order_date descending
select new { ord.id,
date = Tools.toPersianDateTime((DateTime)ord.order_date),
ord.is_final,
ord.status,
ord.image_count,
ord.order_count,
ord.total_price });
Run Code Online (Sandbox Code Playgroud) c# ×9
asp.net ×2
datetime ×2
dictionary ×2
linq ×2
vb.net ×2
.net ×1
inherited ×1
reflection ×1
string ×1
string-split ×1
xml ×1