简体   繁体   English

我在使用此 linq 时遇到问题

[英]I'm having trouble with this linq

I need to get some data out and use it in other level.我需要获取一些数据并在其他级别使用它。 I can't return (c), and I'm looking for a pattern.我不能返回 (c),我正在寻找一种模式。 How can I return that correctly?我怎样才能正确返回?

public string AutoUpdate(string _search)
{
    using (var context = new Phone_BookEntities1())
    {
        var c = from d in context.Cantacts
                where d.Cantact1 == _search
                select d.Cantact1.ToString();
        return c;
    }
}

The Problem is your query is returning IEnumerable<T> where T is the type of Cantact1 but here since you are converting it to string, it will return IEnumerable<string> .问题是您的查询返回IEnumerable<T> ,其中TCantact1的类型,但在这里由于您将其转换为字符串,它将返回IEnumerable<string> But since the return type is string you need to return single item.但是由于返回类型是string您需要返回单个项目。 You can use FirstOrDefault :-您可以使用FirstOrDefault :-

 var c = (from d in context.Cantacts
          where d.Cantact1 == _search
          select d.Cantact1.ToString()).FirstOrDefault();

Or with Lambda syntax:-或者使用 Lambda 语法:-

var c = context.Cantacts.FirstOrDefault(d => d.Cantact1 == _search);
if(c != null)
   return c.Cantact1.ToString();
else
   return String.Empty;  //any default value

I wanted to reiterate Rahul's solution, and expand a bit on the Lambda solution. 我想重申Rahul的解决方案,并进一步扩展Lambda解决方案。

The following demonstrates the declaration and usage of Lambda Expressions. 下面演示了Lambda表达式的声明和用法。

First, ensure you have these namespaces: 首先,确保您具有以下名称空间:

using System;
using System.Linq;
using System.Linq.Expressions;

then 然后

public string AutoUpdate(string _search)
{

    Expression<Func<Cantact, bool>> WhereClause = (d) => d.Cantact1 == _search;
    // thats: System.Linq.Expressions.Expression<System.Func<Cantact, bool>>

    return FirstContact(WhereClause);
    // or
    //return FirstContact((d) => d.Cantact1 == _search));
}

public string FirstContact(Expression<Func<Cantact, bool>> WhereClause) 
{
    using (var context = new Phone_BookEntities1()) 
    {
        //var query = context.Cantacts.FirstOrDefault(WhereClause);
        var query = context.Cantacts.Where(WhereClause).FirstOrDefault();            
        return (query == null) ? String.Empty : query.Cantact1;
    }
}

If _search where a static or constant we wouldn't have to declare WhereClause in the function scope. 如果_search在哪里是staticconstant ,则WhereClause在函数范围内声明WhereClause

As you can see in the code snippet, we moved the functionality of getting the First Contact into it's own function, and AutoUpdate just builds the expression and passes it.. 正如您在代码片段中看到的那样,我们将将“第一联系人”转移到它自己的函数中的功能,而“ AutoUpdate只是构建表达式并传递它。


Note: 注意:

You're returning query.Cantact1 , which is going to be _search .. You should probably consider returning the Model.Cantact entity, or if AutoUpdate is supposed to update entities at some point, return bool or just make the method void , following a Query Command Separation (QCS) Pattern/Responsibility. 您正在返回query.Cantact1 ,它将是_search ..您可能应该考虑返回Model.Cantact实体,或者如果应该在某个时候更新AutoUpdate实体,则返回bool或只是将方法设为void ,然后执行a查询命令分隔(QCS)模式/职责。

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

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