小编Sae*_*eid的帖子

从匿名类型中获取价值

我有一个方法如下:

public void MyMethod(object obj){

  // implement

}
Run Code Online (Sandbox Code Playgroud)

我称之为:

MyMethod(new { myparam= "waoww"});
Run Code Online (Sandbox Code Playgroud)

那么如何实现MyMethod()获取myparam值呢?

编辑

我用这个:

dynamic d= obj; 
string param = d.myparam; 
Run Code Online (Sandbox Code Playgroud)

但错误上升:

'object' does not contain a definition for 'myparam' 
Run Code Online (Sandbox Code Playgroud)

我也使用断点,我看到d有myparam字符串属性.

有没有办法检查动态类型,如果包含这样的任何属性:

if(d.contain(myparam))?
Run Code Online (Sandbox Code Playgroud)

编辑二

这是我的主要代码:

public static MvcHtmlString SecureActionLink(this HtmlHelper htmlHelper, 
         string linkText, string actionName, string controllerName, 
         object routeValues, object htmlAttributes) {


    string areaName = 
         (string)htmlHelper.ViewContext.RouteData.DataTokens["area"];

        dynamic areaObject = routeValues;

        if(areaObject != null && !string.IsNullOrEmpty(areaObject.area))
            areaName = areaObject.area;

// more
}
Run Code Online (Sandbox Code Playgroud)

并称之为:

<p>@Html.SecureActionLink("Secure Link between Areas", …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc anonymous-types c#-4.0 asp.net-mvc-3

15
推荐指数
3
解决办法
2万
查看次数

有没有办法将lambda表达式作为变量或参数传递?

我需要传递lambda查询作为参数,以下代码是示例,我很有兴趣为它找到一个工具,有样本:有些东西是这样的:

var expr1 = Where(n => n > 6).OrderBy(n => n % 2 == 0).Select(n => n);
var expr2 = TakeWhile((n, index) => n >= index));
Run Code Online (Sandbox Code Playgroud)

并使用它像这样:

public void UseLambda<T> (IEnumerable<T> source , lambda Expr){

var items= Expr.Compile(source);

foreach(var item in items)
     Console.Writeline(item.ToString());
}

public void Main(){
    List<int> numbers = new List<int> { 10, 24, 9, 87, 193, 12, 7, 2, -45, -2, 9 };
    var expr1 = Where(n => n > 6).OrderBy(n => n % 2 == 0).Select(n => …
Run Code Online (Sandbox Code Playgroud)

.net c# linq lambda

14
推荐指数
2
解决办法
2455
查看次数

将Identity列添加到SQL Server 2008中的视图

这是我的看法:

Create View [MyView] as
(
Select col1, col2, col3 From Table1
UnionAll
Select col1, col2, col3 From Table2
)
Run Code Online (Sandbox Code Playgroud)

我需要添加一个名为的新列Id,我需要这个列是唯一的,所以我想添加新列作为标识.我必须提到这个视图返回了大量的数据,所以我需要一个性能良好的方法,而且我还使用了两个带有union的select查询,我认为这可能有些复杂,那么你的建议是什么?

sql t-sql sql-server view sql-server-2008

13
推荐指数
1
解决办法
5万
查看次数

ASP.NET MVC实现自定义验证器使用IClientValidatable

在这里问类似的问题,但是在这个问题中我使用了另一个实现,正是这种方式下面的代码显示了我的实现:

模型:

public class Department {

    public long Id { get; set; }

    [IsDateAfter("Date2", true, ErrorMessage = "O My")]
    public DateTime Date1 { get; set; }
    public DateTime Date2 { get; set; }
    public string Name1 { get; set; }
    public string Name2 { get; set; }

}
Run Code Online (Sandbox Code Playgroud)

自定义验证器:

public sealed class IsDateAfter : ValidationAttribute, IClientValidatable {

    private readonly string testedPropertyName;
    private readonly bool allowEqualDates;

    public IsDateAfter(string testedPropertyName, bool allowEqualDates = false)
  {
        this.testedPropertyName …
Run Code Online (Sandbox Code Playgroud)

javascript asp.net-mvc razor unobtrusive-validation asp.net-mvc-3

11
推荐指数
1
解决办法
1万
查看次数

EF代码中的模型n - n关系如何自动生成视图正常工作?

我使用EF Code First并且在nn关系中存在问题,假设我们有一个歌手在某些类型中唱歌,所以我们需要这个模型:Artist,Genre和ArtistsGenres,我将模型定义如下:

这是我的艺术家模型:

public class Artist
{
    public long Id { get; set; }
    public string Name { get; set; }
    public ICollection<Genre> Genres { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

和我的类型模型:

public class Genre
{
    public long Id { get; set; }
    public string Title { get; set; }
    public ICollection<Artist> Artists { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我的上下文类:

public class MusicDB : DbContex
{
    public DbSet<Artist> Artists { get; set; }
    public DbSet<Genre> Genres { get; set; }
    public DbSet<ArtistsGenres> …
Run Code Online (Sandbox Code Playgroud)

.net c# entity-framework ef-code-first

10
推荐指数
2
解决办法
6413
查看次数

使用ASP.NET MVC3在自定义Htmlhelper中查找区域名称和控制器名称

我尝试重写和自定义@Html.ActionLink,在此方法的一个重载中,参数是:

public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, 
                                       string linkText,   string actionName);
Run Code Online (Sandbox Code Playgroud)

我想要像上面这样的东西,还需要找到AreaName和ControllerName而不通过参数传递它,我想使用以下内容:

string controller = ViewContext.RouteData.Values["Controller"];
string area = ViewContext.RouteData.DataTokens["Area"];
Run Code Online (Sandbox Code Playgroud)

但错误上升为:

An object reference is required for the non-static field, method, or property
'System.Web.Mvc.ControllerContext.RouteData.get'
Run Code Online (Sandbox Code Playgroud)

显然我使用静态,那你在寻找区域名称和控制器名称的建议是什么HtmlHelpers

asp.net-mvc static html-helper razor asp.net-mvc-3

10
推荐指数
1
解决办法
7676
查看次数

在ASP.NET MVC 3中以编程方式创建routeValues(匿名类型)

假设我们有一个页面,其中包含一些支持从DB搜索的元素,因此在Post Action中我们需要找到哪些元素是活动的,并根据这些创建路由值,如下所示:

List<Parameter> SearchParameters = GetFilterParameters(collection);


if(SearchParameters.Count > 0)
foreach(Parameter item in SearchParameters) {

    switch(item.Name) {
                    case "Category":
                    CategoryValue= item.Value;
                        break;

                    case "StartDate":
                    StartDateValue= item.Value;
                        break;


                    case "Product":
                    ProductValue= item.Value;
                        break;

                }
return RedirectToAction("Index", new {category = CategoryValue, startdate=StartDateValue, product=ProductValue });
Run Code Online (Sandbox Code Playgroud)

那么有没有办法routeValues动态定义类似下面的伪代码:

var dynamicRoutValues;
foreach(Parameter item in SearchParameters) {

  dynamicRoutValues.Add(item.Name, item.Value)

}

    return RedirectToAction("Index", dynamicRoutValues);
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc dynamic anonymous-types asp.net-mvc-3

9
推荐指数
1
解决办法
3587
查看次数

通过jQuery检查jquery unobtrusive验证是否正确

我使用MVC 3模型验证属性和jquery unobtrusive来显示验证错误消息也在表单提交时使用脚本返回确认.所以我需要检查所有字段是否有效然后返回确认:有些事情如下面的伪脚本:

$('div.FormNeedConfirm form').submit(function () {
    if ($(this).validate() == true) {
        var Message = $('#FormConfirmMessage').val();
        return confirm(Message);
    }
});
Run Code Online (Sandbox Code Playgroud)

但我不知道if条件到底应该是什么.你的建议是什么?

javascript validation jquery unobtrusive-validation asp.net-mvc-3

9
推荐指数
1
解决办法
7754
查看次数

按计数计数的SQL组

这是我的剧本

SELECT COUNT( [Id]) as [Count Of Register],
Tag as [Tag]
FROM [Members]
Group By Tag 
Order By [Count Of Register] Desc;
Run Code Online (Sandbox Code Playgroud)

返回表是这样的:

Count   Tag

550 ----1
550 ----2
545 ----3
545 ----4
545 ----5
Run Code Online (Sandbox Code Playgroud)

所以,这次我需要Count of Tag,Group by这个新的Count字段.

一些返回值如:

2 ---550
3 ---545
Run Code Online (Sandbox Code Playgroud)

有没有办法不使用新表或模板表或任何存储表只是通过查询?

sql t-sql sql-server sql-server-2008

8
推荐指数
1
解决办法
1万
查看次数

具有不同属性名称的 Typescript 映射类型

我使用 Angular 6 + TypeScript 2.7.2 并且我有一个需要ISelect[]类型的组件:

export interface ISelect {
    id: number;
    title: string;
}
Run Code Online (Sandbox Code Playgroud)

所以每次我调用组件时,我都需要传递一个ISelect[]. 例如,我有一个IProject[],我需要将其映射到ISelect[]在另一个示例中,我IClient[]再次需要将其映射到ISelect[],这里是IProject

export interface IProject {
    id: number;
    name: string;
    code: string;
    clientId: number;
    status: number;
    level: number;
    masterKey: number;
    parentId?: number;
}
Run Code Online (Sandbox Code Playgroud)

我需要像这个伪代码一样映射它。

 myprojects: IProject[];
 myselect: ISelect[] = myprojects.Foreach.Map<ISelect>(id: id, title: name);
Run Code Online (Sandbox Code Playgroud)

或者:

export interface IClient {
    code: number,
    fullname: string,
    streetNumber: string,
    streetName: string,
    postalCode: string,
    city: string, …
Run Code Online (Sandbox Code Playgroud)

typescript angular

7
推荐指数
1
解决办法
9463
查看次数