我绑定DataGrid.ItemsSource属性List<PersonDetails>对象.我通过支持Silverlight的WCF服务获取数据.所以这个PersonDetails类是在Web Project中实现的.如果类位于Silverlight项目中,则每个DataGrid的标题文本都会根据需要进行更改.但是我不能在Web服务中使用这个类.唯一的解决方案是将相同的类添加到两个项目中.但是,还有其他方法吗?
这个类看起来像这样:
[DataContract]
public class PersonGeneralDetails
{
// Properties
[DataMember]
[DisplayAttribute(Name = "Sira")]
public int RowNumber { get; set; }
[DataMember]
[DisplayAttribute(Name = "Seriyasi")]
public string SerialNumber { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
似乎在Web项目中不生成属性.我知道我可以使用DataGrid事件更改标题文本.但我想使用属性使其工作.
我必须创建一个方法,用于从具有指定类型的集合中选择firts属性.
我已经创建了这样的方法(为了简洁,我删除了一些部分):
public static IQueryable<TResult> SelectFirstPropertyWithType<T, TResult>(this IQueryable<T> source)
{
// Get the first property which has the TResult type
var propertyName = typeof(T).GetProperties()
.Where(x => x.PropertyType == typeof(TResult))
.Select(x => x.Name)
.FirstOrDefault();
var parameter = Expression.Parameter(typeof(T));
var body = Expression.Convert(Expression.PropertyOrField(parameter, propertyName), typeof(TResult));
var expression = Expression.Lambda<Func<T, TResult>>(body, parameter);
return source.Select(expression);
}
Run Code Online (Sandbox Code Playgroud)
我可以将此方法称为:
List<Person> personList = new List<Person>();
// .. initialize personList
personList.AsQueryable()
.SelectFirstPropertyWithType<Person, int>()
.ToList();
Run Code Online (Sandbox Code Playgroud)
一切正常.
但是,我不想将第一个参数类型设置为Person,因为编译器可以从集合的源中推断出这个参数类型.有没有办法像这样调用这个方法:
.SelectFirstPropertyWithType<int>()
问题是我需要T在我的方法中使用参数,而且我不想Func在运行时使用反射创建.
谢谢.
我有这样的界面:
public interface IFoo
{
decimal Amount { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我有一些视图模型实现它:
public class Foo1 : IFoo
{
[Display(Name = "Foo1 Amount")]
[Range(6, 11)]
public decimal Amount { get; set; }
}
public class Foo2 : IFoo
{
[Display(Name = "Foo2 Amount")]
[Range(1, 5)]
public decimal Amount { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我不想为每个Foo1和 都创建一个新视图Foo2。
所以,我创建了一个具有类型模型的视图IFoo。
@model IFoo
<div>
@Html.LabelFor(x => x.Amount)
@Html.TextBoxFor(x => x.Amount)
@Html.ValidationMessageFor(x => x.Amount)
</div>
Run Code Online (Sandbox Code Playgroud)
但是,它不会创建客户端不显眼的属性,例如Range客户端中的属性。 …
_dbEntities.EmployeeAttendances.Where(x => x.DailyDate.Date.Equals(DateTime.Now.Date)).ToList();
Run Code Online (Sandbox Code Playgroud)
"LINQ to Entities不支持指定的类型成员'Date'.仅支持初始值设定项,实体成员和实体导航属性."
如何根据linq查询中的当前日期获取员工数据?
我从启用Silverlight的WCF服务获取数据并将其绑定到DataGrid ItemSource.但我的ViewModel的构造函数正在获取一个参数.我正在使用MVVM.我希望从xaml传递参数到构造函数.我必须在这里添加什么?这是我在xaml中设置页面的DataContext的部分.
<navigation:Page.DataContext>
<vms:StudentViewModel />
</navigation:Page.DataContext>
Run Code Online (Sandbox Code Playgroud)
这是类的构造函数:
public StudentViewModel(string type)
{
PopulateStudents(type);
}
Run Code Online (Sandbox Code Playgroud)
另外,这是错误:
类型'StudentViewModel'不能用作对象元素,因为它不是公共的,或者没有定义公共无参数构造函数或类型转换器.
为什么这些打印不同的东西?
假设我有这样的课程:
public class ExampleOfFunc
{
public int Addition(Func<int> additionImplementor)
{
if (additionImplementor != null)
return additionImplementor();
return default(int);
}
}
Run Code Online (Sandbox Code Playgroud)
并在Main方法中:
这打印200:
ExampleOfFunc exampleOfFunc = new ExampleOfFunc();
Console.WriteLine("{0}", exampleOfFunc.Addition(
() =>
{
return 100 + 100;
})); // Prints 200
Run Code Online (Sandbox Code Playgroud)
但这打印Prints System.Func'1[System.Int32]:
Console.WriteLine("{0}", new Func<int>(
() =>
{
return 100 + 100;
})); // Prints System.Func`1[System.Int32]
Run Code Online (Sandbox Code Playgroud) 我在我的项目中使用Linq-To-Entity.但是,我在使用Stored Procedure或LINQ to Table or View在某些情况下犹豫不决.不过,我一般喜欢使用LINQ,因为的普莱语法.我搜索过谷歌,但没有找到详细的问题.
我们考虑一下这段代码:
using (NorthwindEntities db = new NorthwindEntities())
{
var custs = from c in db.Customers where c.City == "London" select c;
var edus = from c in db.Educations where c.Education != "2" select c;
// ... and so on
}
Run Code Online (Sandbox Code Playgroud)
问题:
1.它是否为每个查询打开一个新连接?如果是,那么不建议单独使用上述查询?
2.另外,你能否告诉我有什么情况我必须使用存储过程而不是LINQ?
我正在读一本关于高级C#的书.而且,现在我正在阅读这部分内容:
实现基于委托的语法的Linq查询方法的幕后操作.
到目前为止,我看了一下Where,Select,Skip,SkipWhile,Take,TakeWhile方法.并且,我知道Defferred和Immediate执行以及Iterator由这些方法返回的s.
延迟执行是执行模型的模式,通过该模式,CLR确保仅在基于IEnumerable的信息源需要时才提取值.当任何Linq运算符使用延迟执行时,CLR将相关信息(例如原始序列,谓词或选择器(如果有))封装到迭代器中,当使用ToList方法从原始序列中提取信息时将使用该迭代器.或ForEachmethod或在C#中手动使用底层的GetEnumeratorand MoveNextmethods.
现在让我们看看这两个例子:
IList<int> series = new List<int>() { 1, 2, 3, 4, 5, 6, 7 };
// First example
series.Where(x => x > 0).TakeWhile(x => x > 0).ToList();
// Second example
series.Where(x => x > 0).Take(4).ToList();
Run Code Online (Sandbox Code Playgroud)
当我提出断点并调试这两个语句时,我可以看到一个区别.
TakeWhile()当一个项满足Where语句中的条件时执行的方法.但是,Take方法并非如此.
第一个声明:

第二个声明:

你能解释一下为什么吗?
我正在我的项目中使用匿名函数.直到知道我在想,C#编译器只使用在同一个类中使用匿名方法的代码生成一个方法.但是,在IL中反编译此代码之后,我看到CLR创建了一个新类.
public class Comparer
{
public delegate int Greater(int a, int b);
public int Great(Greater greater, int a, int b)
{
return greater(a, b);
}
}
static void Main(string[] args)
{
int valueOfA = 11,
valueOfB = 23,
valueOfC = 42;
Comparer comparer = new Comparer();
Console.WriteLine("The greater is \t:{0}",
comparer.Great(delegate(int a, int b)
{
int[] numbers = new int[] { a, b, valueOfC };
return Math.Max(Math.Max(a, b), valueOfC);
},
valueOfA, valueOfB));
}
Run Code Online (Sandbox Code Playgroud)
这是Main方法的反编译IL代码:
.method private …Run Code Online (Sandbox Code Playgroud) 我最近的任务要求我遵守以下条件
“所有方法都有显式的后置条件,而那些具有参数前置条件的方法”
我已经阅读了一些网页,试图解释前后条件,但似乎可以理解它们,有人可以向我解释它们的含义,用法以及如何编写它们吗?
谢谢
(顺便说一句,我正在学习的语言是C#)