来自Linq查询的调用方法

时间:2010-05-05 11:59:08

标签: c# wpf linq

我正在使用Linq查询和调用方法Like ..

oPwd = objDecryptor.DecryptIt((c.Password.ToString())

它将返回空值。

意味着这不起作用。

我如何解决这个问题。

谢谢..

var q =
    from s in db.User
    join c in db.EmailAccount on s.UserId equals c.UserId
    join d in db.POPSettings 
        on c.PopSettingId equals d.POPSettingsId
    where s.UserId == UserId && c.EmailId == EmailId
    select new
    {
        oUserId = s.UserId,
        oUserName = s.Name,
        oEmailId = c.EmailId,
        oEmailAccId = c.EmailAccId,
        oPwd = objDecryptor.DecryptIt(c.Password.ToString()),
        oServerName = d.ServerName,
        oServerAdd = d.ServerAddress,
        oPOPSettingId = d.POPSettingsId,
    };

3 个答案:

答案 0 :(得分:11)

如果是LINQ-to-SQL或Entity Framework。您需要将其分解为步骤(因为它无法在数据库中执行)。例如:

var q = from s in db.User
        join c in db.EmailAccount on s.UserId equals c.UserId
        join d in db.POPSettings on c.PopSettingId equals d.POPSettingsId
        where s.UserId == UserId && c.EmailId == EmailId
        select new
        {
            oUserId = s.UserId,
            oUserName = s.Name,
            oEmailId = c.EmailId,
            oEmailAccId = c.EmailAccId,
            oPwd = c.Password,
            oServerName = d.ServerName,
            oServerAdd = d.ServerAddress,
            oPOPSettingId = d.POPSettingsId,
        };

然后使用AsEnumerable()打破后端商店的“组合”:

var query2 = from row in q.AsEnumerable()
        select new
        {
            row.oUserId,
            row.oUserName,
            row.oEmailId,
            row.oEmailAccId,
            oPwd = objDecryptor.DecryptIt(row.oPwd),
            row.oServerName,
            row.oServerAdd,
            row.oPOPSettingId
        };

答案 1 :(得分:1)

var q = from s in db.User
    join c in db.EmailAccount on s.UserId equals c.UserId
    join d in db.POPSettings on c.PopSettingId equals d.POPSettingsId
    where s.UserId == UserId && c.EmailId == EmailId
    select new
    {
        oUserId = s.UserId,
        oUserName = s.Name,
        oEmailId = c.EmailId,
        oEmailAccId = c.EmailAccId,
        oPwd = c.Password,
        oServerName = d.ServerName,
        oServerAdd = d.ServerAddress,
        oPOPSettingId = d.POPSettingsId,
    };

foreach (var item in q)
        {
            item.oPwd = objDecryptor.DecryptIt(row.oPwd),
        }

我们也可以使用foreach循环来更新单个属性。不需要在下一个查询中选择所有属性。

答案 2 :(得分:0)

这与Linq查询无关。你需要调试方法objDecryptor.DecryptIt