簡體   English   中英

如何在我的Dot net核心項目中執行原始SQL查詢? 或如何將其翻譯為Linq?

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

我很難找到有關執行原始查詢的任何信息,我不知道如何將其轉換為LINQ或什至有可能。

我的DBSet中有ContactRecords,但我真的不希望如此,我只需要進行多次計數即可。

我需要執行多個計數,並使用我創建的“ usp_Parse_domain_name_v5”數據庫函數:

這是我要在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;

為什么要用linq解決? 您還可以編寫存儲過程並調用它。 那更容易。 如果查詢太復雜,我將始終使用限定過程而不是linq。 這是一個例子; 點網核心中的存儲過程

好吧, https: //stackoverflow.com/users/7529020/yy選擇的答案肯定對我有幫助!

我創建了一個模型並將其添加到上下文中:

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; }
    }

也創建了存儲過程

然后我在控制器上要做的就是:

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

並在我的頂部視圖中: @model IEnumerable <YOURCONTEXT.Models.GatewaysImpressionsAndConversions>

然后循環:

 <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>

非常感謝! 如果不是@YY的答案,我現在會撞在牆上。

希望這可以幫助需要幫助的人,

最好的祝福

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM