简体   繁体   English

使用Linq,存在检查数据库中记录的最有效方法

[英]The most efficient way to check records in a database exist, using Linq

I am using Linq and to check whether or not a record exists in the database and that it is the latest version. 我正在使用Linq并检查数据库中是否存在记录,并且它是最新版本。 I can do this using Linq 我可以使用Linq做到这一点

bool query = db.foo.Any(x => x.Id == list.Id);

And then checking to see if this produced a true or false result. 然后检查这是否产生了正确或错误的结果。

The issue I have with this approach is that if I want to check all the attributes of the table to determine if the existing data differs from the new copy then it requires me to specify a mapping for each attribute and list item. 这种方法的问题在于,如果我要检查表的所有属性以确定现有数据是否与新副本不同,则需要我为每个属性和列表项指定一个映射。

Is there a way I can iterate through or are there other alternatives available? 有没有一种我可以迭代的方法,还是有其他替代方法可用?

x refers to the Entity framework object that is representing the DB x表示代表数据库的Entity框架对象
list is a list that is mapped 1 to 1 to the Entity Framework object. list是一个列表,该列表将1:1映射到Entity Framework对象。

foo query = db.foo.Where(x => x.Id == list.Id).FirstOrDefault();

if(query != null)
{
    properties = query.GetType().GetProperties();
    foreach(PropertyInfo prop in properties)
    {
        if (list.GetType().GetProperty(prop.Name) != null)
        {
            var val1 = list.GetType().GetProperty(prop.Name).GetValue(list);
            var val2 = prop.GetType().GetProperty(prop.Name).GetValue(prop);
            // do your checking here
        }
    }
}

If I understand you correctly, you want a sort of for each loop to iterate over all the columns for a row, and check that they match the corresponding data from the list - is that correct? 如果我对您的理解正确,那么您希望for each循环都遍历一行的所有列,并检查它们是否与列表中的相应数据匹配-是正确的吗?

The short answer is: Sorry, I don't think you can do that (at least not without using some reflection tricks, which might be rather messy, and will not perform as well as you probably want). 简短的答案是:对不起,我认为您不能做到这一点(至少不能不使用一些反射技巧,这可能会很杂乱,并且无法达到您想要的效果)。

With SQL you can write: SELECT * FROM table (...) , but there is no such option in Linq; 使用SQL可以编写: SELECT * FROM table (...) ,但是Linq中没有这样的选项; you need to specify the names of each column explicitly. 您需要明确指定每个列的名称。

What you could and probably should do instead, is what Jon Skeet mentioned briefly in a comment: Add a version number, or possibly a timestamp that you can check. 您可能并且可能应该做的是Jon Skeet在评论中简短提到的内容:添加一个版本号,或者可能是您可以检查的时间戳。 Write your logic in such a way that you can be certain that if the version is correct/updated, then the row is valid. 以这样一种方式编写逻辑,可以确定如果版本正确/已更新,则该行有效。

You can then check the validity of a specific row by looking at it's version number. 然后,您可以通过查看特定行的版本号来检查其有效性。 You could for instance do something like this: 例如,您可以执行以下操作:

// Does a row with specified ID exist and the newest version exist:
bool isRowValid = db.foo.Any(x => x.Id == list.Id && 
                                  x.VersionNr == list.newestVersionNr);
bool chechExists = db.foo.Exists(x => x.Id == list.Id);

这将根据需要返回布尔值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM