这个c#代码可能不是最有效的,但得到我想要的.
如何在F#代码中完成同样的事情?
string xml = " <EmailList> " +
" <Email>test@email.com</Email> " +
" <Email>test2@email.com</Email> " +
" </EmailList> ";
XmlDocument xdoc = new XmlDocument();
XmlNodeList nodeList;
String emailList = string.Empty;
xdoc.LoadXml(xml);
nodeList = xdoc.SelectNodes("//EmailList");
foreach (XmlNode item in nodeList)
{
foreach (XmlNode email in item)
{
emailList += email.InnerText.ToString() + Environment.NewLine ;
}
}
Run Code Online (Sandbox Code Playgroud) 下面是两个返回相同数据的查询.除了风格,我不确定哪个更好.
哪些因素会影响这些查询?使用一种风格而不是另一种风格有什么好处?
样品1
var x = from s in db.Surveys
join sq in db.Survey_Questions on s.ID equals sq.Survey_ID
join q in db.Questions on sq.Question_ID equals q.ID
join qg in db.Question_Groups on q.ID equals qg.Question_ID
where s.Type_ID.Equals(typeID) & s.Type.Equals(type)
select new { question = sq.Question, status = sq.Status, grp = qg };
Run Code Online (Sandbox Code Playgroud)
样本2
var x = db.Surveys.Where(s => s.Type_ID.Equals(typeID) & s.Type.Equals(type))
.Join(db.Survey_Questions,
s => s.ID,
sq => sq.Survey_ID,
(s, sq) => new
{
question = sq.Question,
status = sq.Status
})
.Join(db.Question_Groups, …Run Code Online (Sandbox Code Playgroud) 问:如何计算呈现MVC页面并在母版页上显示时间所需的总时间.
在Asp.net Web Form中,我创建了一个Base页面类,如下所示:
public class PageBase : System.Web.UI.Page
{
private DateTime startTime = DateTime.Now;
private TimeSpan renderTime;
public DateTime StartTime
{
set { startTime = value; }
get { return startTime; }
}
public virtual string PageRenderTime
{
get
{
renderTime = DateTime.Now - startTime;
return renderTime.Seconds + "." + renderTime.Milliseconds + " seconds";
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我会在我的母版页上调用方法,如下所示:
<div id="performance">
<% =PageRenderTime %>
</div>
Run Code Online (Sandbox Code Playgroud)
问:我如何使用MVC框架完成同样的事情?
问:使用MVC框架,我在哪里设置首次创建页面的开始时间?
手动创建lambda表达式时,我得到一个"参数不在范围内"的异常.
关于我做错了什么的任何想法?
public class OtherType
{
public string First_Name { get; set; }
public string Last_Name { get; set; }
}
static void Main(string[] args)
{
Expression<Func<OtherType, bool>> l2 =
p => p.First_Name == "Bob";
l2.Compile(); // Works
PropertyInfo property =
typeof(OtherType).GetProperty("First_Name");
ParameterExpression para =
Expression.Parameter(typeof(OtherType), "para");
ConstantExpression right =
Expression.Constant("Bob", typeof(string));
MemberExpression left =
Expression.Property(Expression.Parameter(typeof(OtherType), "para"), property);
BinaryExpression binary =
Expression.MakeBinary(ExpressionType.Equal, left, right);
Expression<Func<OtherType, bool>> l =
Expression.Lambda<Func<OtherType, bool>>(binary, new ParameterExpression[] { para });
l.Compile(); // Get a 'Lambda …Run Code Online (Sandbox Code Playgroud) 我想遍历一个字符串值数组并构建一个 linq 表达式
列表中的每个项目都被 OR 运算在一起。
string[] search = new string[]{"A", "B", "C"};
foreach (string item in filterValues)
{
searchQuery = searchQuery.Where(s => s.Name.Contains(item));
}
Run Code Online (Sandbox Code Playgroud)
上面的代码搜索“A”和“B”和“C”
我想搜索“A”或“B”或“C”。
我知道如何用 Linq 做到这一点,但我想使用扩展方法来完成同样的事情。
我正在移植CTP 1.9.6.8中的一些旧F#代码
该代码使用List.first:
List.first (fun x -> if x.Date = d then Some(x) else None)
Run Code Online (Sandbox Code Playgroud)
List.first已被弃用.用于实现相同功能的当前方法是什么.
我查看了发行说明,但没有找到任何具体的变更参考.
任何帮助将不胜感激.
我试图通过移植一些Haskell代码来教我自己的F#.
特别是我试图移植这里显示的倒计时问题
我试图在F#中创建以下Haskell类型:
data Op = Add | Sub | Mul | Div
data Expr = Val Int | App Op Expr Expr
Run Code Online (Sandbox Code Playgroud)
在F#中我认为Op类型定义如下:
type Op = | Add | Sub | Mul | Div
Run Code Online (Sandbox Code Playgroud)
我遇到了Expr类型的问题.
如何创建递归类型?从这个SO问题看起来,人们无法在F#中创建Expr类型.
还有什么是'App'类型的F#等价物,它将Op类型应用于Expr类型.
如果无法直接移植此代码,有人可能会建议替代数据结构.
我想在F#中做这个C#代码
string[] a = new string[5];
string b = string.Empty;
a[0] = "Line 1";
a[2] = "Line 2";
foreach (string c in a)
{
b = c + Environment.NewLine;
}
Run Code Online (Sandbox Code Playgroud) 我有一个C#类返回一个List,使用System.Collections.Generic列表而不是F#List
我想迭代列表以找到一个对象或找不到它.这是我在C#中的表现.我将如何在F#中完成类似的事情
foreach (AperioCaseObj caseObj in CaseList)
{
if (caseObj.CaseId == "")
{
}
else
{
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个Web用户控件,在客户端上生成以下html.
我想使用JavaScript引用特定的RadioButton控件.
我不知道如何做到这一点因为asp.net生成了RadioButton ID.
例如,我给出RadioButton ID = 1_RadioButton
asp.net生成一个ID = ctl00_ContentPlaceHolder1_Material_1_RadioButton
这个javascript代码怎么能找到Radio Button控件?
<script type="text/javascript">
$(document).ready(function() {
if (document.getElementById("1_RadioButton").checked) {
$("#1_other").hide();
}
});
</script>
Run Code Online (Sandbox Code Playgroud)
以下是asp.net为客户端生成的内容.
<input id="ctl00_ContentPlaceHolder1_Material_1_RadioButton" type="radio" name="ctl00$ContentPlaceHolder1$Material$Academic" value="1_RadioButton" onclick="javascript:$('#1_other').show('fast');" />
<label for="ctl00_ContentPlaceHolder1_Material_1_RadioButton">Yes</label>
<input id="ctl00_ContentPlaceHolder1_Material_2_RadioButton" type="radio" name="ctl00$ContentPlaceHolder1$Material$Academic" value="2_RadioButton" checked="checked" onclick="javascript:$('#1_other').hide('fast');" />
<label for="ctl00_ContentPlaceHolder1_Material_2_RadioButton">No</label>
<input id="ctl00_ContentPlaceHolder1_Material_3_RadioButton" type="radio" name="ctl00$ContentPlaceHolder1$Material$Academic" value="3_RadioButton" onclick="javascript:$('#1_other').hide('fast');" />
<label for="ctl00_ContentPlaceHolder1_Material_3_RadioButton">Uncertain</label>
<div id='1_other'>
<input name="ctl00$ContentPlaceHolder1$Material$4_TextBox" type="text" value="bbb chabng" id="ctl00_ContentPlaceHolder1_Material_4_TextBox" />
</div>
Run Code Online (Sandbox Code Playgroud) 我试图将这个haskell函数移植到F#
subs :: [a] -> [[a]]
subs [] = [[]]
subs (x:xs) = ys ++ map (x:) ys
where
ys = subs xs
Run Code Online (Sandbox Code Playgroud)
例
潜艇[1,2,3]
收益:
[[],[3],[2],[2,3],[1],[1,3],[1,2],[1,2,3]]
返回列表的所有子序列,由排除或包括每个元素的所有可能组合给出
....
我遇到'where'语句的问题,它递归地生成另一个列表'ys'.
我也不确定我将谓词'(x :)'正确地移植到'(有趣的是我 - >我)'.
这是我能弄清楚的F#声明.
let rec subs list =
match list with
| [] -> [[]]
| x::xs -> List.map (fun i -> i) xs
Run Code Online (Sandbox Code Playgroud)
任何帮助或方向将不胜感激.
f# ×6
c# ×3
haskell ×2
linq ×2
asp.net ×1
asp.net-mvc ×1
iqueryable ×1
javascript ×1
jquery ×1
lambda ×1
performance ×1
string ×1
xml ×1