在linq中使用left with charindex c#

时间:2016-03-01 03:50:32

标签: c# linq

我有一个包含描述列的表,我想选择不同的第一个单词。这是我将使用的SQL:

select distinct left(description,charindex(' ',description)-1)  from tableA

如何将其转换为LINQ查询语法?

2 个答案:

答案 0 :(得分:1)

简单的解决方案

    <Host name="localhost"  appBase="webapps"
        unpackWARs="true" autoDeploy="true">

    <!-- SingleSignOn valve, share authentication between web applications
         Documentation at: /docs/config/valve.html -->
    <!--
    <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
    -->

    <!-- Access log processes all example.
         Documentation at: /docs/config/valve.html
         Note: The pattern used is equivalent to using pattern="common" -->
    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
           prefix="localhost_access_log." suffix=".txt"
           pattern="%h %l %u %t &quot;%r&quot; %s %b" />
           <Context docBase="D:\temp\uploads\vtour" path="/MySharedHTML"/>

  </Host>

答案 1 :(得分:1)

在LINQ to Entities中,您可以将DbFunctions.LeftString.IndexOf Method (String)结合使用(根据CLR Method to Canonical Function Mapping支持),如下所示

var result = db.TableA
    .Select(r => DbFunctions.Left(r.Description, r.Description.IndexOf(" ")))
    .Distinct();