我遇到了一些我的代码.我有一个控制器,我打电话从我的数据库中检索公司列表.但是,当我测试它时,我得到一个错误,指出状态:LINQ to Entities无法识别方法System.String ToString()
方法,并且此方法无法转换为存储表达式.
我似乎无法找到解决这个问题的方法,并且有人建议我使用foreach
循环.
public List<SelectListItem> GetCompanyList()
{
using (var ctx = new Risc_Track_beEntities())
{
var companies = ctx.tblCodeBrokerComps.Where(comp => comp.BrokerCompStatus == 1);
var activeBrokerLocs = companies.Select(b => new SelectListItem
{
Value = b.BrokerCompID.ToString(),
Text = b.BrokerComp
});
return activeBrokerLocs.ToList<SelectListItem>();
}
}
Run Code Online (Sandbox Code Playgroud) 我们通过将新记录写入临时文件,然后用临时文件替换旧文件来执行大型文本文件的更新.一个严重缩写的版本:
var tpath = Path.GetTempFileName();
try
{
using (var sf = new StreamReader(sourcepath))
using (var tf = new StreamWriter(tpath))
{
string line;
while ((line = sf.ReadLine()) != null)
tf.WriteLine(UpdateLine(line));
}
File.Delete(sourcepath);
File.Move(tpath, sourcepath);
}
catch
{
File.Delete(tpath);
throw;
}
Run Code Online (Sandbox Code Playgroud)
如果有任何异常抛出异常(找不到文件,没有权限),原始文件保持不变,这就是我们想要的.
但是,代码存在以下问题:
是否存在真实的情况,Delete
但是Move
失败了?这将删除原始数据和更新数据.这会很糟糕.
最常见的故障是源文件从另一个应用程序打开,并且Delete
失败.这意味着将丢弃所有更新工作.有没有办法查看源文件在开始时是否可删除,如果没有则放弃更新?
我们让用户将Windows资源管理器摘要属性(如标题或注释)放在文件上.删除文件时会丢弃这些内容.有没有办法将旧文件的Summary属性复制到新文件?我们应该这样做吗?
我有一个名为myvals的表,其中包含以下字段:
ID number
-- -------
1 7
2 3
3 4
4 0
5 9
Run Code Online (Sandbox Code Playgroud)
从第2行开始,我想添加前一行号的数字.所以,我的最终结果将是这样的
ID number
-- ------
1 7
2 10
3 7
4 4
5 9
Run Code Online (Sandbox Code Playgroud) 我想知道ApplicationException
当用户违反某些业务规则时,是否建议使用它来返回应用程序错误.例如:
public void validate(string name, string email)
{
int count1 = (from p in context.clients
where (p.name == clients.name)
select p).Count();
if (count1 > 0)
throw new ApplicationException("Your name already exist in the database");
int count2 = (from p in context.clients
where (p.email == clients.email)
select p).Count();
if (count2 > 0)
throw new ApplicationException("Your e-mail already exist in the database");
}
Run Code Online (Sandbox Code Playgroud)
这是一个好的或坏的策略?如果不是,那会是更好的方法?
我想生成一个半笛卡尔半极坐标图。例如,高斯曲线的一半采用极坐标格式,另一半采用笛卡尔格式。哪个是最好的方法?
我尝试过共享 y 轴,但未能成功为一个图采用两种不同类型的格式。
import matplotlib.pyplot as plt
import numpy as np
theta = np.linspace(0,(np.pi)/2)
r = np.sin(theta)
fig = plt.figure()
ax = fig.add_subplot(211, polar=True)
c = ax.scatter(theta, r, c=r, s=5, cmap='hsv', alpha=0.85)
ax.set_thetamin(0)
ax.set_thetamax(90)
ax1=fig.add_subplot(212) #, sharey='ax')
fig, (c, d) = plt.subplots(ncols=2,sharey=True)
d=ax1.scatter(theta,r)
plt.show()
Run Code Online (Sandbox Code Playgroud) 我试图像这样将一些值插入到SQL CE数据库中,但没有任何反应。我做错了什么?
string strConn = Properties.Settings.Default.SqlConnectionString;
using (SqlCeConnection con = new SqlCeConnection(strConn))
{
con.Open();
using (SqlCeCommand cmd = new SqlCeCommand("insert into CustTable(ID, Name) values (@Val1, @val2)", con))
{
cmd.Parameters.AddWithValue("@Val1", customer.ID);
cmd.Parameters.AddWithValue("@Val2", customer.Name);
cmd.CommandType = System.Data.CommandType.Text;
cmd.ExecuteNonQuery();
}
}
Run Code Online (Sandbox Code Playgroud) 我不明白为什么元素的顺序是重要的而不是属性的情况.
任何人都可以提供合理的理由吗?
element addressBook {
element card {
attribute name { text },
attribute email { text }
}*
}
Run Code Online (Sandbox Code Playgroud)
在XML中,属性的顺序传统上并不重要.RELAX NG遵循这一传统.上述模式将匹配两者
<card name="John Smith" email="js@example.com"/>
Run Code Online (Sandbox Code Playgroud)
和
<card email="js@example.com" name="John Smith"/>
Run Code Online (Sandbox Code Playgroud)
相反,元素的顺序很重要.模式
element card {
element name { text },
element email { text }
}
Run Code Online (Sandbox Code Playgroud)
不匹配
<card><email>js@example.com</email><name>John Smith</name></card>
Run Code Online (Sandbox Code Playgroud)
那一个怎么样:
如果元素或属性的子元素与数据类型模式匹配,则元素或属性的完整内容必须与该数据类型模式匹配.不允许具有允许部分内容与数据类型模式匹配的模式,以及与另一模式匹配的另一部分.例如,不允许使用以下模式:
element bad {
xsd:int,
element note { text }
}
Run Code Online (Sandbox Code Playgroud)
但是,这没关系:
element ok {
xsd:int,
attribute note { text }
}
Run Code Online (Sandbox Code Playgroud)
请注意,此限制不适用于文本模式.
我目前正在制作一个涉及为考试创建模板的计划.在我允许用户向考试添加问题的功能中,我需要确保仅使用存储其数据所需的内存.经过对各种输入函数(getc,scanf等)之间的差异的大量研究,我已经设法这样做了,我的程序似乎正在运行,但我关注一件事.这是我的函数的代码,我在相关的行上发表了评论:
int AddQuestion(){
Question* newQ = NULL;
char tempQuestion[500];
char* newQuestion;
if(exam.phead == NULL){
exam.phead = (Question*)malloc(sizeof(Question));
}
else{
newQ = (Question*)malloc(sizeof(Question));
newQ->pNext = exam.phead;
exam.phead = newQ;
}
while(getchar() != '\n');
puts("Add a new question.\n"
"Please enter the question text below:");
fgets(tempQuestion, 500, stdin);
newQuestion = (char*)malloc(strlen(tempQuestion) + 1); /*Here is where I get confused*/
strcpy(newQuestion, tempQuestion);
fputs(newQuestion, stdout);
puts("Done!");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
令我感到困惑的是,我尝试运行相同的代码,但只需稍加修改即可测试幕后发生的事情.我尝试从我malloc
那里删除+ 1 ,我放在那里因为strlen
只计算但不包括终止字符,我假设我想要包含终止字符.那仍然没有顺利.所以我尝试运行它,但是使用-1而不是印象,这样做会删除终止字符之前的任何内容(换行符,正确吗?).不过,它在不同的行上显示了所有内容.所以现在我有些困惑,并怀疑我对字符数组如何工作的了解.有人可以帮助澄清这里发生的事情,或者可能为我提供一个资源,可以更详细地解释这一切吗?