S2K
S2K

Reputation: 1285

How do I query an Azure storage table with Linq method syntax in .net core?

I want to use linq expression to query data against Azure table storage.
Currenlty i am using TableQuery.GenerateFilterCondition which does not provide the flexibility that i want.
Code i am using is :

CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
            cloudTable = tableClient.GetTableReference("Data");

            //QueryFilters.GetQueryExpression(searchParameters);
            //string expBody = ((LambdaExpression)whereClause).Body.ToString();
            string filter1 = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "PBIFinalData");
            var condition = TableQuery.GenerateFilterCondition("Number", QueryComparisons.Equal, "98880302");
            string filterRange = TableQuery.CombineFilters(filter1, TableOperators.And, condition);
            TableQuery<MCIOData> query = new TableQuery<MCIOData>().Where(filterRange);

I want to combine multiple filter conditions and each condition may have one or multiple values to find within a table.

I have created the lambda expressions but to which function should i pass that lambda expression to get the result

.

    internal static Expression<Func<Data, bool>> GetQueryExpression(List<FieldTemplate> searchParameters)
    {
        var param = Expression.Parameter(typeof(Data));

        Expression exp = null;

        if (searchParameters.Count() == 1)
        {
            exp = GetQuery(param, searchParameters[0]);
        }
        else
        {
            exp = GetQuery(param, searchParameters[0]);

            if (exp != null)
            {
                for (int i = 1; i < searchParameters.Count(); i++)
                {
                    exp = Expression.And(exp, GetQuery(param, searchParameters[i]));
                }
            }
        }

        return Expression.Lambda<Func<MCIOData, bool>>(exp, param);
    }

Upvotes: 0

Views: 1611

Answers (1)

Dogu Arslan
Dogu Arslan

Reputation: 3384

I don't think the client sdk for the old azure table storage has a linq provider. I also don't think Entity framework supports the old table storage api either, these would have been your 2 easy options for using native linq queries if either of them was there. So what you can do is to implement your own custom Linq provider, some samples are here: https://weblogs.asp.net/mehfuzh/writing-custom-linq-provider https://blogs.msdn.microsoft.com/mattwar/2008/11/18/linq-building-an-iqueryable-provider-series/

or use the new Cosmos Db Table Storage that natively supports Linq to Sql: https://learn.microsoft.com/en-us/azure/cosmos-db/sql-query-linq-to-sql

Upvotes: 1

Related Questions