gpt4 book ai didi

How can pass the model property as an argument in a method(如何在方法中将模型属性作为参数传递)

转载 作者:bug小助手 更新时间:2023-10-24 22:48:39 24 4
gpt4 key购买 nike



I am trying to pass model class property as an argument to the method IsNameDuplicateAsync and use the model class property name from the argument. I am trying to check the Product Description is already in the database. So how can I rewrite the code using the property name as from the argument

我尝试将模型类属性作为参数传递给IsNameDuplicateAsync方法,并使用该参数中的模型类属性名称。我正在尝试检查数据库中是否已有产品说明。那么,如何使用参数中的属性名称重写代码呢


if (await IsNameDuplicateAsync<Product>("Description"))
{
var tested = "Ok";
}
public async Task<bool> IsNameDuplicateAsync<TModel>(string columnName) where TModel : class
{
return await Set<TModel>().AnyAsync(x => x.Name == "TestName");
}

I am trying to bring the code as given below

我正在尝试将下面给出的代码


return await Set<TModel>().AnyAsync(x => x.Description== "TestName");

更多回答

What is Set in this case? What is TModel? It looks like you aren't using any models but trying to execute an ORM query with EF Core. The whole point of using ORMs is to give the impression of working with objects, eg db.Customers.AnyAsync(c=>c.Name==somename) not tables, rows, and strings. If you don't want to work that way, it's a lot easier to either write the SQL query or use a micro-ORM like Dapper.

在这种情况下设置了什么?什么是TModel?看起来您没有使用任何模型,而是试图使用EF Core执行ORM查询。使用ORMS的全部目的是为了给人一种使用对象的感觉,例如,db.Customers.AnyAsync(c=>c.Name==somename),而不是表、行和字符串。如果您不想以这种方式工作,那么编写SQL查询或使用像Dapper这样的微ORM要容易得多。

Which EF version are you using? Newer versions make it easier to work with ad-hoc queries. LINQ-to-Entities works with expressions, so you may be able to pass a Expression<Func<TSource,bool>> expression to your method, if it's simple enough. For complex expressions you may have to use LinqKit

您使用的是哪个EF版本?较新的版本使处理即席查询变得更容易。Linq-to-Entities使用表达式,因此您可以将表达式>传递给您的方法,如果它足够简单的话。对于复杂的表达式,您可能必须使用LinqKit

x => EF.Property<object>(x, columnName) == "TestName". See docs: EF.Property method.

X=>EF.Property(x,ColumnName)==“测试名称”。请参阅docs:EF.Property方法。

优秀答案推荐

You can use reflection to access the property by its name passed as an argument.
For example:

您可以使用反射通过作为参数传递的名称来访问属性。例如:


if (await IsPropertyDuplicateAsync<Product>("Description", "TestName"))
{
var tested = "Ok";
}

public async Task<bool> IsPropertyDuplicateAsync<TModel>(string propertyName, string propertyValue) where TModel : class
{
var propertyInfo = typeof(TModel).GetProperty(propertyName);
if (propertyInfo == null)
{
throw new ArgumentException($"The property '{propertyName}' does not exist in the type '{typeof(TModel).Name}'.");
}

return await Set<TModel>().AnyAsync(x => (string)propertyInfo.GetValue(x) == propertyValue);
}


Use EF.Property method. With it, the code turns out to be the simplest.

使用EF.Property方法。有了它,代码就变得最简单了。


public async Task<bool> IsNameDuplicateAsync<TModel>(string columnName, object value)
where TModel : class
{
return await Set<TModel>().AnyAsync(x => EF.Property<object>(x, columnName) == value);
}

Call:

电话:


IsNameDuplicateAsync<Product>("Description", "TestName"))

There is a disadvantage here that you can make a typo in the property name, which will lead to an error at runtime.

这里有一个缺点,您可能会在属性名称中输入错误,这将在运行时导致错误。


I can suggest another option that gives a stricter typing of the code.

我可以建议另一个选项,提供更严格的代码键入。


public async Task<bool> IsNameDuplicateAsync<TModel>(Expression<Func<TModel, bool>> expr)
where TModel : class
{
return await Set<TModel>().AnyAsync(expr);
}

Call:

电话:


IsNameDuplicateAsync<Product>(x => x.Description == "TestName")

更多回答

This won't translate into SQL. It's about Entity Framework.

这不会转换成SQL。它是关于实体框架的。

24 4 0