小编Gra*_*Fox的帖子

从实体框架核心中包含的属性中选择特定属性

我使用Entity Framework Core 2.0并拥有这两个类:

新闻

public class News
{
    public int Id { get; set; }
    public string Title{ get; set; }
    public string Text { get; set; }
    public DateTime ReleaseDate{ get; set; }
    public int AuthorID{ get; set; }
    public Author Author{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)

作者

public class Author 
{
    public Author ()
    {
     News = new List<News>();
    }

    public int Id { get; set; }
    public string Username{ get; set; }
    public string Firstname{ get; set; …
Run Code Online (Sandbox Code Playgroud)

entity-framework-core

8
推荐指数
2
解决办法
6408
查看次数

无法将lambda表达式转换为委托类型

我有这样的方法:

public ICollection<T> GetEntitiesWithPredicate(Expression<Func<T, bool>> predicate)
{
           // ...
}
Run Code Online (Sandbox Code Playgroud)

我在另一个类中进行方法调用

service.GetEntitiesWithPredicate(x => x.FoobarCollection.Where(y => y.Text.Contains(SearchText)));
Run Code Online (Sandbox Code Playgroud)

但我总是得到这个错误:

Lambda expression cannot be converted to '<typename>' because '<typename>' is not a delegate type
Run Code Online (Sandbox Code Playgroud)

为了让这项工作,我需要改变什么?

编辑:

我使用Entity Framework 6,如果我使用Any()而不是Where(),我总是只得到1个结果...我想将表达式传递给我的EF实现:

    public ICollection<T> GetEntriesWithPredicate(Expression<Func<T, bool>> predicate)
    {
        using (var ctx = new DataContext())
        {
            return query.Where(predicate).ToList();
        }
    }
Run Code Online (Sandbox Code Playgroud)

c# lambda delegates

6
推荐指数
1
解决办法
530
查看次数

不能在泛型方法中隐式转换类型错误

我的通用方法有问题:

    public ReadOnlyObservableCollection<T> GetReadOnlyObjectsFromDB<T>() 
    {
        var typeofT = typeof(T);
        if (typeofT.GetType() == typeof(Customer))
        {
            return new ReadOnlyObservableCollection<Customer>
                  (new ObservableCollection<Customer>(dbContext.Customers));
        }
        else if(typeofT.GetType() == typeof(Article))
        {
            return new ReadOnlyObservableCollection<Article>
                  (new ObservableCollection<Article>(dbContext.Articles));
        }
    }
Run Code Online (Sandbox Code Playgroud)

我总是得到这个错误:

Cannot implicitly convert type 'System.Collections.ObjectModel.ReadOnlyObservableCollection<Customer>' to 'System.Collections.ObjectModel.ReadOnlyObservableCollection<T>'

和文章相同.我认为用这种方法清楚我想要的但我不知道我的错误是什么......

感谢您的帮助和新年快乐!

c# generics

2
推荐指数
1
解决办法
509
查看次数

了解c#中的引用行为

我对以下代码有疑问:

    static void Main(string[] args)
    {
        var msg = new Message()
        {
            Description = new Description() { Key = 1, Value = "Foo"},
            ID = 1,
            Name = "MyMessage"
        };
        changeDescription(msg.Description);
    }

    static void changeDescription(Description descrToChange)
    {
        descrToChange = new Description() { Key = 2, Value = "Foobar" };
    }
Run Code Online (Sandbox Code Playgroud)

为什么我的msg在方法调用后有Description(Key = 1,Value ="Foo")?

类别:

class Message
{
    public Description Description { get; set; }

    public int ID { get; set; }

    public string Name { get; set; }
} …
Run Code Online (Sandbox Code Playgroud)

c#

2
推荐指数
1
解决办法
103
查看次数

Asp-fallback-href总是触发

我玩asp核心并希望回退到我的本地bootswatchSlate.css,这是在我无法从cdn访问bootstrap的wwwroot文件夹中:

Layout.cshtml

<link rel="stylesheet" 
          href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
          asp-fallback-href="css/bootswatchSlate.css"
          asp-fallback-test-class="hidden"
          asp-fallback-test-property="visibility"
          asp-fallback-test-value="hidden"/>
Run Code Online (Sandbox Code Playgroud)

我总是得到bootswatchSlate.css虽然cdn是直播的,我可以到达它.我的问题在哪里?

css asp.net

2
推荐指数
1
解决办法
792
查看次数

合并 TagHelper 语句

我使用 asp core 2.0 MVC 并且经常使用这样的形式的代码:

<form asp-controller="Foo" asp-action="Bar">                
  <div class="form-group">
     <label asp-for="Age"></label>
     <input asp-for="Age" class="form-control"/>
     <span asp-validation-for="Age" class="text-danger"></span>
  </div>

  <div class="form-group">
     <label asp-for="Name"></label>
     <input asp-for="Name" class="form-control"/>
     <span asp-validation-for="Name" class="text-danger"></span>
  </div>
</form>
Run Code Online (Sandbox Code Playgroud)

有没有办法将标签、输入和跨度标签组合在 TagHelper 类中,以编写如下内容:

  <div class="form-group">
    <foo for="Age">
  </div>
Run Code Online (Sandbox Code Playgroud)

这将产生以下结果:

 <label asp-for="Age"></label>
 <input asp-for="Age" class="form-control"/>
 <span asp-validation-for="Age" class="text-danger"></span>
Run Code Online (Sandbox Code Playgroud)

避免一遍又一遍地为我的属性编写这 3 个常见标签?

先感谢您。

编辑:

嘿伊斯玛,谢谢你的回答。您的代码生成简单的

<label asp-for="Age"></label>
<input asp-for="Age" class="form-control"/>
<span asp-validation-for="Age" class="text-danger"></span>
Run Code Online (Sandbox Code Playgroud)

html。

但我想要“已处理”版本,例如

<input asp-for="Age"/>
Run Code Online (Sandbox Code Playgroud)

<input type="number" data-val="true" data-val-range="The field Age must be between 0 and 100." …
Run Code Online (Sandbox Code Playgroud)

c# asp.net razorengine

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