c# - 使用 LINQ 在运行时从数据库中获取数据

标签 c# linq

我希望能够编写和更新我的数据库,向我的服务发送一个对象,然后让该服务动态决定要写入或更新到数据库的对象类型。

这是我静态执行时所做的(更新示例)

switch ((string)property.GetValue(oLinq, null))
                {
                    case "news":
                        t_news oNews = (t_news)oLinq;                            
                        List<t_news> newsList = (from n in oDB.t_news where n.ID.Equals(oNews.ID) select n).ToList();
                        t_news oNe = newsList[0];
                        i = 0;
                        foreach (System.Reflection.PropertyInfo p in oNews.GetType().GetProperties().ToList())
                        {
                            if (p.GetValue(oNews, null) != null)
                            {
                                typeof(t_news).GetProperties().ToList()[i].SetValue(oNe, p.GetValue(oNews, null), null);
                            }
                            i++;
                        }
                        oNe.dLastUpdate = DateTime.Now;
                        oDB.SubmitChanges();
                        oLinq = (object)oNews;
                        break;
return oLinq;

oDB 是我的 DataContext。 oLinq 只是一个包含我要更新的数据的对象。 它包含一个字段,我在其中指定需要更新哪种表。 我使用 switch case 来确定指定该表。 我现在有 4 个不同的案例,在这些案例中我的做法几乎相同。

但我希望能够动态地执行此操作,这样我就不必将所有这些代码重写 4 次,并且能够在将来添加新表。

这就是我正在尝试的:

List<string> alsTableNames = (from tables in oDB.Mapping.GetTables() select tables.TableName).ToList();
                foreach (string sTableName in alsTableNames)
                {
                    if (String.Compare(sTableName.Substring(6), (string)property.GetValue(oLinq, null)) == 0)
                    {
                        string sBasicType = sTableName.Replace("dbo.", "");
                        string sType = "RegisterService." + sBasicType;
                        Type tType = Type.GetType(sType);
                        oLinq = Convert.ChangeType(oLinq, tType);

这可以将 oLinq 对象转换为我想要的类型。 但是,将数据从数据库中取出来用新数据替换它是行不通的。 我基本上需要一种以动态方式执行此操作的方法:

List<t_news> newsList = (from n in oDB.t_news where n.ID.Equals(oNews.ID) select n).ToList();
                        t_news oNe = newsList[0];

类似于:

List<//type of the table I want> list = (from t in //table where t.//firstproperty.Equals(oLinq.getType().getProperties().toList()[0]) select t.ToList();
                        object oNewObj = list[0];

有什么想法吗?

最佳答案

您是否考虑过使用 Dynamic LINQ

使用它你可以做类似的事情:

// Use reflection to figure out what type of entity you need to update
var tYourEntityType = /* your code to get the type of the object you will be working with */;

// Get the entity you need to update querying based on a dynamic where clause
string propetyNameToSeach = "firstproperty"; // set this dynamically to the name of the property
string propertyValueToCompare = "5"; // set this dynamically to the value you want to compare to
var entityToUpdate = youDataContext.GetTable<tYourEntityType>().Where(propetyNameToSeach + " = " + propertyValueToCompare).FirstOrDefault();

关于c# - 使用 LINQ 在运行时从数据库中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10212675/

相关文章:

asp.net - Linq "equals"关键字

c# - linq连接问题

c# - 如何在属性中搜索特定值?

c# - c# 中的 getResponse 不工作。没有回复回来

c# - Angular Controller 未被调用

c# - 将 WCF Restful 服务添加到现有的 ASP.Net 网站

c# - 保存本地数据库的最简单方法

c# - 将 Expression<Func> 选择器绑定(bind)到 linq 查询

c# - Linq 查询帮助

c# - 是否可以向 Rx.Net Timeout 运算符添加自定义消息