使用下面的代码(来自我拼凑在一起的控制台应用程序),我在我的数据表中添加了七列.完成后,如何为每列设置数据类型?例如,数据表的第1列将具有标题"ItemNum",我想将其设置为Int.我在网上查看了一些示例,但大多数都显示了一次创建列标题和列数据类型,如下所示:
loadDT.Columns.Add("ItemNum", typeof(Int));
Run Code Online (Sandbox Code Playgroud)
在我的程序中,该列已经有了一个名称.我只是想做这样的事情(不是实际的代码):
loadDT.Column[1].ChangeType(typeof(int));
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止的代码(它给列命名):
// get column headings for datatable by reading first line of csv file.
StreamReader sr = new StreamReader(@"c:\load_forecast.csv");
headers = sr.ReadLine().Split(',');
foreach (string header in headers)
{
loadDT.Columns.Add(header);
}
Run Code Online (Sandbox Code Playgroud)
显然,我对此很陌生,但我很努力学习.有人能指出我正确的方向吗?谢谢!
我有一个包含以下数据的数据集
Name Value Percent
0-3 months 0 0
3-12 months 0 0
1-5 years 1234.12 28
5-10 years 13144.11 68
10-15 years 0 0
Over 15 years 1233.44 14
Other Income 2245.12
Run Code Online (Sandbox Code Playgroud)
当我尝试
foreach (DataRow dr in dsMaturity.Tables[0].Select("name not like 'Other%'"))
{
TotalValue += double.Parse(dr["Value"].ToString());
}
Run Code Online (Sandbox Code Playgroud)
编辑:实际上,类似于上面的代码.我正在使用类似的循环将数据添加到显示存储桶,最终写入图表.
foreach (DataRow dr in dsMaturity.Tables[0].Rows)
{
//Add to the display bucket
}
Run Code Online (Sandbox Code Playgroud)
我得到的数据排序如下:
0-3 months
10-15 years
1-5 years
3-12 months
5-10 years
Over 15 years
Run Code Online (Sandbox Code Playgroud)
为什么?如何将数据排除?这是至关重要的,因为我在图表对象中显示数据.我在这里错过了什么吗?
我需要比较c#中的两个数据表来找出差异.这两个数据表具有相同的模式.最好的方法是什么?可以使用linq查询吗?如果是,怎么样?
<h:form id="formId">
<p:wizard id="wizardId">
<p:tab id="tabId">
<p:dataTable id="tableId">
<p:column>
<h:commandLink value="remove" update=""/>
</p:column>
</p:dataTable>
</p:tab>
</p:wizard>
</h:form>
Run Code Online (Sandbox Code Playgroud)
我只需更新<p:dataTable>没有整个表单.我尝试使用@form,@parent,:formId:wizardId:tabId:tableId,但没有人正在为我想.当我使用时@form,它正在检查我不需要做的验证.
我怎样才能做到这一点?
我有一个困惑
场景:
我想创建一个DataTable的副本,以添加到另一个DataSet.有两种方法(AFAIK):
1. Make a Copy using DataTable.Copy()
2. Make a Deep Clone using
public static T DeepClone<T>(this T source)
{
if (!typeof(T).IsSerializable)
throw new ArgumentException("The type must be serializable.", "source");
// Don't serialize a null object, simply return the default for that object
if (Object.ReferenceEquals(source, null))
return default(T);
IFormatter formatter = new BinaryFormatter();
Stream stream = new MemoryStream();
using (stream)
{
formatter.Serialize(stream, source);
stream.Seek(0, SeekOrigin.Begin);
return (T)formatter.Deserialize(stream);
}
}
Run Code Online (Sandbox Code Playgroud)
我的困惑:
DataTable.Copy()在内部使用DeepClone或一些其他的逻辑是什么?我在下面编写了一个方法,使用反射在.NET应用程序中加载几个强类型的数据表.如果我按原样运行,一切正常 - 包括没有抛出异常.但是如果我使用注释部分(保持其他所有内容相同),那么我将无法启用此处描述的约束错误:在此处输入链接描述.
如果我查看errors数组中的内容,它总是如下:
"Column 'AEDelegateName' does not allow DBNull.Value."
Run Code Online (Sandbox Code Playgroud)
并且错误的ItemArray将类似于:
[0] = {}
[1] = "Some Value"
Run Code Online (Sandbox Code Playgroud)
这让我感到惊讶,因为我只希望脚本中的一列选择1列,而不是像上面所示的2列.我也想象这很接近问题,因为其中一个似乎是空的.
我的脚本不会返回null,我可以快速直观地确认它,并在我使用的查询中说出NOT NULL之类的内容.
private void GetData(string query, Component tableAdapter)
{
OracleCommand command = new OracleCommand();
command.Connection = conn;
command.CommandText = query;
command.CommandType = CommandType.Text;
command.CommandTimeout = 3000;
OracleDataReader reader = command.ExecuteReader(CommandBehavior.SingleResult);
MethodInfo[] methods = tableAdapter.GetType().GetMethods();
MethodInfo getDataMethod = tableAdapter.GetType().GetMethod("GetData");
DataTable table = (DataTable)getDataMethod.Invoke(tableAdapter, null);
Type[] paramTypes = new Type[] { table.GetType() };
MethodInfo updateMethod …Run Code Online (Sandbox Code Playgroud) 我有一堆DataTables需要转换为object[,]数组(而不是 object[][]数组).在性能方面,最有效的方法是什么?
我知道我可以通过我的建设做这个object[dt.Rows.Count, dt.Columns.Count]最初,然后通过行循环和解析每一行到阵列中的地方,但我相当肯定还有其他方法,如使用LINQ或System.Data特定的功能,如dataRow.ToItemArray()它可更有效率.
我DataTables是可变大小,除了字符串之外,还包含需要格式化的日期和数字.
例如,如果包含我的一个数据表
Id Name Date Value 1 Rachel 1/1/2013 00:00:00 100.0000 2 Joseph 3/31/2012 00:00:00 50.0000 3 Sarah 2/28/2013 00:00:00 75.5000
那么我想要一个object[,]包含完全相同数据的数组(理想情况下是标题),但是需要格式化的日期和值
arr[x,0] = row[x].Field<int>("Id");
arr[x,1] = row[x].Field<string>("Name");
arr[x,2] = row[x].Field<DateTime>("Date").ToString("M/d/yy");
arr[x,3] = row[x].Field<decimal>("Value").ToString("C2"); // Currency format
Run Code Online (Sandbox Code Playgroud) 我在primefaces 5.0上使用datatable的filterFunction方法.我想按列标题上的日期范围过滤生日.
在浏览器控制台上,我收到此错误:
<?xml version="1.0"
encoding="utf-8"?><partial-response><error><error-name>java.lang.ClassCastException</error-name><error-message><![CDATA[javax.faces.component.UIPanel
cannot be cast to
javax.faces.component.ValueHolder]]></error-message></error></partial-response>
Run Code Online (Sandbox Code Playgroud)
数据表:
<p:dataTable var="person" value="#{testDateRange.persons}"
id="personTable" paginator="true" styleClass="customTableStyle" editable="true"
rows="10" resizableColumns="true"
emptyMessage="No persons"
filteredValue="#{testDateRange.filteredPersons}"
widgetVar="dateRangeWidget" >
<p:column id="nameId" filterBy="name" sortBy="name" filterMatchMode="contains" headerText="Name">
<h:outputText value="#{person.name}" />
</p:column>
<p:column id="birthdayId" headerText="birthday" filterBy="birthday" filterFunction="#{testDateRange.filterByDate}">
<f:facet name="filter">
<p:calendar id="from" value="#{testDateRange.dateFrom}" styleClass="customCalendar" pattern="dd/MM/yyyy">
<p:ajax event="dateSelect" oncomplete="PF('dateRangeWidget').filter()" update="personTable"/>
</p:calendar>
<p:calendar id="to" value="#{testDateRange.dateTo}" styleClass="customCalendar" pattern="dd/MM/yyyy">
<p:ajax event="dateSelect" oncomplete="PF('dateRangeWidget').filter()" update="personTable"/>
</p:calendar>
</f:facet>
<h:outputText value="#{person.birthday}" >
<f:convertDateTime pattern="dd/MM/yyyy"/>
</h:outputText>
</p:column>
</p:dataTable>
Run Code Online (Sandbox Code Playgroud)
豆:
@Component("testDateRange")
@Scope("session")
public class TestDateRangeBean {
private …Run Code Online (Sandbox Code Playgroud) 我正在处理带有可点击行的javascript数据表.每一行都有onclick函数,但是在我的一列中我有不同的链接打开jquery对话框,在这列上我要禁用行点击方法,怎么做?这是我实现行点击的功能
$(rec' tbody').on( 'click', 'tr', function () {
});
Run Code Online (Sandbox Code Playgroud) 因为使用SQL Server 2016+执行R脚本,是否可以获得多个表?让我们从互联网上随机抽取一个简单的例子(不需要发布我的文章就不会过分复杂化):
EXEC sp_execute_external_script
@language =N'R',
@script=N'OutputDataSet<-InputDataSet',
@input_data_1 =N'SELECT 1 AS hello'
WITH RESULT SETS (([hello] int not null));
Run Code Online (Sandbox Code Playgroud)
发布在这里.
这里结果作为单个表返回.假设我对数据进行了各种计算,现在我想返回多个表.
例如:
a<-InputDataSet
b<-InputDataSet + 5
Run Code Online (Sandbox Code Playgroud)
这些将返回两个不同的表作为结果.现在我无法想出任何好的方法来返回两个单独的表中的数据,因为它只返回一个表.显然,我可以这样返回:
OutputDataSet<-data.frame(a, b)
Run Code Online (Sandbox Code Playgroud)
但是处理不同的功能和不同的数据很快就会变得非常麻烦.例如,我使用一个函数lm.现在,一个数据集将被计算估计值,另一个数据集将是参与该等式的每个列的系数.当然,我当然可以加入这两个数据表并稍后处理它们,但在许多情况下输出结果变得很大.
程序的参数如下:..., @output_data_1_name但是没有@output_data_2_name,等等因此我没有看到方法.也许有可能创建它OutputDataSet所以它拥有多个表?如果是这样的话 - 由于我缺乏经验,我在R中并不知道这种方式.
tldr; 是否可以返回多个结果集,或者我唯一的解决方案是在R代码中手动构造输出,所以我总是得到一个?