如何根据LINQ和实体框架的条件进行项目属性?

发布于 2025-01-17 22:36:39 字数 629 浏览 0 评论 0原文

我想为 LINQ List 声明类属性的变量。 我在数据库中有大约 100 个属性,我想根据用户选择更改输出数据。例如,如果用户选择“A”,我想从 EntityFramework 获取“A”值。

这是我的示例代码:

List<ReadTime> report = bWDiagnosticEntities2.CA190STEST
   .Where(x => (x.Date > StartDate) && (x.Date <= EndDate))
   .Select(x => new ReadTime 
     { 
         Date = x.Date,
         kody5 = x.kodyBledow5NetVarA 
     })
   .OrderBy(x => x.Date)
   .ToList();

我想更改为:

kody5 = x.kodyBledow5NetVarA

其中

kody5 = myVariable

myVariable 取决于用户发送的模型。

I would like to declare variable of class property for LINQ List.
I have about 100 properties in Database and i would like to change output data depends on user choice. For example if user chose "A" i would like to get "A" values from EntityFramework.

Here is my sample code:

List<ReadTime> report = bWDiagnosticEntities2.CA190STEST
   .Where(x => (x.Date > StartDate) && (x.Date <= EndDate))
   .Select(x => new ReadTime 
     { 
         Date = x.Date,
         kody5 = x.kodyBledow5NetVarA 
     })
   .OrderBy(x => x.Date)
   .ToList();

I would like to change:

kody5 = x.kodyBledow5NetVarA

to

kody5 = myVariable

Where myVariable depends on model sent from User.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠我空喜 2025-01-24 22:36:39

我认为最简单的方法是将查询和投影零件分开。
查询将由EF在数据库端运行,并检索所有字段。

然后,您进行内存中的投影,并根据用户的选择注入属性(用户必须从列表中选择现有属性)。

在EF查询中要记住的一件重要的事情是,当您调用tolist()时,您将执行实际的数据库查询。然后,所有其他语句将在内存中执行。

// This will perform an EF query, filtered by the where predicate
var reportEntities = yourContext.YourEntityModelDbSet
     // save resources on EF context if you don't need to update entities later
    .AsNoTracking()
    // Filter as much as you can in this predicate, for it will
    // be translated into SQL by EF
    .Where(x => x.Date > StartDate && x.Date <= EndDate)
    // Calling ToList will retrieve all entites matching the where predicate
    // including all their fields (take care of data size)
    .ToList();
    
// Then, use reflection to retrieve the data
// according to user's choice
var userChoice = GetUserInput(); // will return the property name as a string

var report = reportEntities
    .Select(x => new ReadTime 
     { 
         Date = x.Date,
         // Using reflection to get the value of a property
         // Given its name.
         Kody5 = x.GetType().GetProperty(userChoice)?.GetValue(x, null)
     })
     .ToList();

?。操作员由于不存在的任何例外
财产选择。它可以保证您不会得到运行时的例外,但是
您永远不会知道财产选择是否错误。

如果数据卷是一个问题(并且可能会因为您告诉您有一百个字段),请在tolist()之前执行第一个select(),其中无论如何,您都将严格地投射所需的字段。

I think the simplest approach would be to split your query and projection parts.
Query will be run by EF on database side, and retrieve all the fields.

Then you do your projection in-memory, and inject the property based on user's choice (the user is required to pick an existing property from a list).

One important thing to remember in an EF query, is that when you call ToList(), you will perform the actual database query. Then all the further statements will be performed in memory.

// This will perform an EF query, filtered by the where predicate
var reportEntities = yourContext.YourEntityModelDbSet
     // save resources on EF context if you don't need to update entities later
    .AsNoTracking()
    // Filter as much as you can in this predicate, for it will
    // be translated into SQL by EF
    .Where(x => x.Date > StartDate && x.Date <= EndDate)
    // Calling ToList will retrieve all entites matching the where predicate
    // including all their fields (take care of data size)
    .ToList();
    
// Then, use reflection to retrieve the data
// according to user's choice
var userChoice = GetUserInput(); // will return the property name as a string

var report = reportEntities
    .Select(x => new ReadTime 
     { 
         Date = x.Date,
         // Using reflection to get the value of a property
         // Given its name.
         Kody5 = x.GetType().GetProperty(userChoice)?.GetValue(x, null)
     })
     .ToList();

The ?. operator swallows any exception due to a non-existing
property choice. It guarantees you won't get a runtime exception, but
you'll never know if the property choice is wrong.

If the data volume is an issue (and it might, since you told you have like a hundred fields), perform a first Select() before the ToList(), where you will project strictly just the fields you will need, in any case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文