简体   繁体   English

如何在我的Dot net核心项目中执行原始SQL查询? 或如何将其翻译为Linq?

[英]How to execute raw SQL Query in my Dot net core project? Or how to translate it to Linq?

I'm having a hard time finding anything about executing a raw query, I have no idea how to translate this to LINQ or if it is even possible. 我很难找到有关执行原始查询的任何信息,我不知道如何将其转换为LINQ或什至有可能。

I have ContactRecords in my DBSet but I don't really want that I guess, I just need to do multiple counts.. raw query.. 我的DBSet中有ContactRecords,但我真的不希望如此,我只需要进行多次计数即可。

I need to do multiple COUNTs and use a DB function i created "usp_Parse_domain_name_v5": 我需要执行多个计数,并使用我创建的“ usp_Parse_domain_name_v5”数据库函数:

This is the Query I'm trying to execute in my DOT NET CORE app: 这是我要在DOT NET CORE应用中尝试执行的查询:

SELECT
    dbo.usp_Parse_domain_name_v5(ContactRecord.URL),
    COUNT(ContactRecord.ContactId) AS TOTAL_IMPRESSIONS,
    COUNT(DISTINCT ContactAttribute.ContactAttributeValue) AS UNIQUE_IMPRESSIONS,
    COUNT(DISTINCT LEADS.LEAD_ID) AS CONVERSIONS,
    MAX(convert(varchar(10), ContactRecord.CallStartDateTime, 102)) AS 'DATE'
FROM
    ContactAttribute 
    LEFT JOIN ContactRecord ON ContactRecord.ContactId = ContactAttribute.ContactId AND ContactAttribute.ContactAttributeName = 'IP'
    LEFT JOIN LEADS 
    ON LEADS.LEAD_ID = ContactRecord.ContactId
    AND LEADS.EMAIL IS NOT NULL AND LEADS.PHONENUMBER IS NOT NULL 
    AND LEADS.FIRSTNAME IS NOT NULL AND LEADS.LASTNAME IS NOT NULL
WHERE
    convert(varchar(10), ContactRecord.CallStartDateTime, 102) = '2019.05.16'
GROUP BY
    dbo.usp_Parse_domain_name_v5(URL)
ORDER BY 
    COUNT(ContactRecord.ContactId) DESC;

Why do you want to solve with linq? 为什么要用linq解决? You can also write a stored procedure and call that. 您还可以编写存储过程并调用它。 That is even easier. 那更容易。 If query is too complex I always use strored procedures instead of linq. 如果查询太复杂,我将始终使用限定过程而不是linq。 Here is an example; 这是一个例子; Stored Procedure in dot net core 点网核心中的存储过程

Alright, the selected answer by https://stackoverflow.com/users/7529020/yy definitely helped me! 好吧, https: //stackoverflow.com/users/7529020/yy选择的答案肯定对我有帮助!

I created a model and added it to my context: 我创建了一个模型并将其添加到上下文中:

GatewaysImpressionsAndConversions GatewaysImpressionsAndConversions

    public partial class GatewaysImpressionsAndConversions
    {


        public string DOMAIN { get; set; }
        public int TOTAL_IMPRESSIONS { get; set; }
        public int UNIQUE_IMPRESSIONS { get; set; }
        public int CONVERSIONS { get; set; }
        public string DATE { get; set; }
    }

created the Stored procedure too 也创建了存储过程

and then all i had to do on the controller was: 然后我在控制器上要做的就是:

 var result = _context.GatewaysImpressionsAndConversions.FromSql("GetGatewaysImpressionsAndConversionsByDay '2019.05.16'");
            return View("Statistics", await result.ToListAsync());

and in my view at the top: @model IEnumerable <YOURCONTEXT.Models.GatewaysImpressionsAndConversions> 并在我的顶部视图中: @model IEnumerable <YOURCONTEXT.Models.GatewaysImpressionsAndConversions>

and then the loop: 然后循环:

 <table class="table" style="display: none" id="results">
        <thead>
            <tr>
                <th>
                    Domain
                </th>
                <th>
                    Total Impressions
                </th>
                <th>
                    Unique Impressions
                </th>
                <th>
                    Conversions
                </th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model)
            {
                <tr>
                    <td>@item.DOMAIN</td>
                    <td>@item.TOTAL_IMPRESSIONS</td>
                    <td>@item.UNIQUE_IMPRESSIONS</td>
                    <td>@item.CONVERSIONS</td>
                </tr>
            }
        </tbody>
    </table>

Thanks alot! 非常感谢! If it wasn't for @YY's answer I would be hitting my head on a wall right now. 如果不是@YY的答案,我现在会撞在墙上。

Hope this helps someone else in need, 希望这可以帮助需要帮助的人,

Best regards 最好的祝福

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

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